I have printed some text using println!
and now I need to clear the terminal and write the new text instead of the old. How can I clear all the current text from terminal?
I have tried this code, but it only clears the current line and 1
is still in the output.
fn main() { println!("1"); print!("2"); print!("\r"); }
A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.
When using the bash shell, you can also clear the screen by pressing Ctrl + L .
You can send a control character to clear the terminal screen.
fn main() { print!("{}[2J", 27 as char); }
Or to also position the cursor at row 1
, column 1
:
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
print!("\x1B[2J\x1B[1;1H");
This will clear the screen and put the cursor at first row & first col of the screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With