How to really clear the terminal?
I can issue the clear command or press Ctrl + L to clear the current Ubuntu terminal, but this just shifts previous output upwards and if you use mouse scroll or PgUP and PgDown keys it’s hard to distinguish where the output of previous command ends and output of current command begins. Is there a way to really clear the terminal so I won’t see previous command results?
To clarify the MacOS commands: cmd.K — clear terminal, can’t scroll up || ctrl.L — clear screen, scroll up for history || multiple ctrl.L — clear multiple screens. Can see the empty prompt for each clear, and will be able to see history prior to the number of clears
12 Answers 12
Yes, the command you’re looking for is
In contrast to clear , or Ctrl + L , reset will actually completely re-initialise the terminal, instead of just clearing the screen. However, it won’t re-instantiate the shell (bash). That means that bash’s state is the same as before, just as if you were merely clearing the screen.
As @Ponkadoodle mentions in the comments, this command should do the same thing more quickly:
- You can set a Keyboard Shortcut to reset the terminal, as explained by towolf.
- If you’re running Kubuntu, and your terminal is Konsole, you need to go to Edit → Clear history, since reset doesn’t work the same way there, as UncleZeiv notes.
Personally I don’t mind that something is left «up» along the scroller when I hit ^L . I don’t see it (that’s what I want!), I don’t feel it, and if suddenly I would need to restore what was there — no problem. reset in contrast is much harder tool — I’ll use it when something went wrong with the terminal — like, when weird escape sequences accidentally ruined the display altogether.
@ulidtko: it does matter when you run consecutive sessions of programs with tons of output. If you scroll back it’s easy to get confused about which execution printed something. I know perfectly well that more sophisticated solutions could be used in these cases, but nonetheless it’s a scenario that comes about pretty often in quick & dirty debugging sessions.
There’s also tput reset which visibly does the same thing, but completes instantaneously (whereas reset can take up to about 2 seconds before the prompt reappears).
I was looking for this for a while and I found some genius that posted this:
Clears the whole screen buffer, very clean. Works on OS X and believe it works fine on most *nix terminals.
For curious, this part ‘\e[3J’ is a terminal escape command.
Great find! And for those who want the Bash-style reset on mac: nano ~/.bashrc and add alias reset=»clear && printf ‘\e[3J'»
be careful aliasing reset — it does more than just clear the terminal. for example, if you accidentally print a binary file with random garbage that corrupts the terminal (e.g. disables echoing of typed characters), reset can usually fix that. of course aliasing is fine as long as you know how to \override an alias-shadowed command.
You can also assign a shortcut in gnome-terminal by going to Edit → Keyboard Shortcuts. I use Shift + Ctrl + Alt + C .
+1. I always have the Menubar hidden; so even after years and years of using gnome, I never thought to look for this. Thanks 🙂
“Reset” do nothing for me, and “Reset and Clear” clear everything but does not re‑display the prompt. None of these menu entries behave like the “reset” command from a terminal.
As @Hibou57 said, my keyboard shortcut for «Reset» doesn’t seem to do anything. Is this functionality broken in 14.04?
@Hibou57 «Reset and Clear» isn’t expected to re-display the prompt, but if you hit Enter afterwards it should re-display the prompt. Does that work for you?
Cross posting my answer from stackoverflow.
Use the following command to do a clear screen instead of merely adding new lines .
yes that’s a ‘printf’ on the bash prompt.
You will probably want to define an alias though.
Explanation
So this becomes c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.
Edit
Here are a few other ways of doing it.
printf "\ec" #\e is ESC in bash echo -en "\ec" #thanks @Jonathon Reinhart. # -e Enable interpretation of of backslash escapes # -n Do not output a new line
The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer.
Or perhaps use the following alias on KDE.
alias cls='clear && echo -en "\e[3J"'
I got the scroll-back clearing command from here.
This has the same effect as launching a new terminal.
When using putty, after running reset , I can still scroll up and see previous stuff. Is this an issue with PuTTY rather than reset ?
@Bira No, reset doesn’t clear the scrollback on macOS 10.14 Mojave. You could try seq 100; reset to see scrollback still there. On macOS terminal, Cmd+K can clear screen and scrollback.
My favorite is printf «\ec» . This can also be printf «\033c» or printf «\x1bc» . That is an ansi escape sequence that miraculously clears the screen and buffer for the terminal output (for most standard terminals I have worked in it seems — I know it works in such as gnome-terminal, terminator, xterm, etc. on Ubuntu-like Linuxes)
I know this works in Linux Mint and Ubuntu 14.04, so I don’t know why people are appending and prepedning things like clear && echo -ne «\033c . printf «\ec» has always worked for me.
Additionally, in my .bashrc I have a binding like this:
Now I press ctrl t all the time.
Do you know how could a mapping be created using the Windows key ? This snippet is really great, works exactly like Command-K on mac. Thank you.
@Niloct you can see if the terminal supports it with xev (just type xev in a terminal inside an x-session). From what I could tell, urxvt doesn’t support it; while I didn’t try in a tty, I am confident it would not be supported. I also doubt xterm or gnome-terminal would support this. It’s tricky with ansi-escapes. Cheers and sorry for late reply.
Clear a terminal screen for real [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Using the clear command on the terminal only fools the user into thinking the screen has been cleared. you can still see output from the previous commands when you scroll using the mouse. This makes life difficult when you are drowning in a tsunami of text. Various solutions (escape code etc.) which can be found on the Internet are only variations of what the clear command already does. So how do you clear the contents of a terminal in Linux for real?
I’d categorize this as «software tools commonly used by programmers» (mentioned in the FAQ as valid).
What you’re really asking is «How can I clear the terminal’s scroll-back buffer?» which is independent of the shell (Bash) or Ubuntu.
@spiderplant0 probably because AskUbuntu is the right place for this — at this time. Didn’t exist when this was asked, so it got closed as off topic, even though that isn’t the case.
That’s a more general question, affecting not only Ubuntu or bash, as @Dennis noted. I’d change the topic «Clear the Ubuntu bash screen for real» —> «Clear a terminal screen for real»
There are many different terminal types with which you can run Bash (the term «bash terminal» is meaningless). «Clearing» isn’t applicable to all of them — sometimes, the nearest approximation is to tear the paper and bin/shred/burn/destroy the bit you don’t want.
12 Answers 12
Use the following command to do a clear screen instead of merely adding new lines .
yes that’s a ‘printf’ on the bash prompt.
You will probably want to define an alias though.
Explanation
So this becomes c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.
Edit
Here are a few other ways of doing it.
printf "\ec" #\e is ESC in bash echo -en "\ec" #thanks @Jonathon Reinhart. # -e Enable interpretation of of backslash escapes # -n Do not output a new line
The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer.
Or perhaps use the following alias on KDE.
alias cls='clear && echo -en "\e[3J"'
I got the scroll-back clearing command from here.
This is actually terminal specific. «\033c» is ESC c which is the VT-XXX escape sequence for «Full Reset (RIS)». Almost all terminals people actually use these days are VT compatible, but if you ever find yourself using a weird terminal, this might not work. @vpit3833’s answer is more likely to work assuming TERM is set correctly.
printf is a Bash builtin (it’s true that it’s also a separate binary, but builtins have precedence and most modern shells have printf ).
@SDX2000 OK . I know you specified Ubuntu, and I assumed that these would behave similar on all «modern» terminal emulators. I initially tested on my MAC’s terminal and it did not reset there, but it did reset on my Centos Linux.
$0.02 a few years later, but i’m a student at CU. Asked my operating systems professor and he said this was an example of ANSI escape sequence: en.wikipedia.org/wiki/ANSI_escape_code This is an example of in-band signalling.
The solution for KDE is a solution for xterm and terminals that support xterm’s escape sequences. The official list of xterm escape sequences is at invisible-island.net/xterm/ctlseqs/ctlseqs.html. (If you want to learn more about terminal escape sequences, see ANSI escape sequence).
Try reset . It clears up the terminal screen but the previous commands can be accessed through arrow or whichever key binding you have.
Thanks! But it clears every thing including the prompt. See my answer for a solution which doesn’t do this.
@SDX2000 Both clear the prompt, and then the shell generates a new one. The one disadvantage to reset is that it seems to be a bit slower (probably because it does more than just emit ESC c) but it’s more portable.
@SDX2000 reset is also handy for those cases where your terminal gets badly mangled because you killed something (or catted a binary file) and it left your term in a mangled state. Ever get into a state where your prompt shows up but not your typing, and when you hit enter the new prompt shows up next to the previous prompt rather than below it? reset fixes that. That’s actually all I ever use it for. I’ve never had a need/desire to clear my scroll-back buffer.