How to really clear the terminal?
I can issue the
clearcommand 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?
Yes, the command you're looking for is
In contrast to
clear, or Ctrl+L,
resetwill 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 @Wallacoloo mentions in the comments, this command should do the same thing more quickly:
From the other answers:
Probably doesn't matter for most users, but the behavior of `reset` is dependent on the terminal.
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.
@UncleZeiv colored prompt anchors usually help me to quickly find the needed spot. Though this is all just habits.
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).
This causes my putty.exe window to resize, would be nice to clear without that happening.
For anyone finding this answer via Google while they're actually on macOS & iTerm 2, it's possible to alias `reset` like `alias reset="reset && printf '\33c\e[3J'"` to get the same behavior.
I was looking for this for a while and I found some genius that posted this:
clear && printf '\e[3J'
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.
Best answer. Even purges **tmux history** (as in *Ctrl+b PgUp*; `reset` fails to do so; tested in rxvt-unicode/urxvt; tmux-specific alternative: `clear && tmux clearhist`).
Thanks, that's super useful! What does `printf '\e[3J'` mean? How does it clean the buffer?
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.
This is the only answer that works to clear Putty scrollback when accessing some SSH via Winders.
@FilipSpiridonov: This is a terminal escape code, similar to the codes that control color of text in your terminal. Some more info at this other answer: https://superuser.com/questions/555554/putty-clear-scrollback-from-commandline
Works on the Linux Sybsystem on Windows 10! I aliased `cls` to this command, so as not to mess with `clear` or `reset`.
Among all answers here, only this one work exactly list cmd-k on mac OS. FYI, I am using Mohave 10.14.3.
@Filip Spiridonov, `\e[3J` is the escape sequence that "Erase Saved Lines, xterm." (i.e. the scroll-back). Reference: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html (search for `CSI Ps J`)
You can also assign a shortcut in gnome-terminal by going to Edit → Keyboard Shortcuts. I useShift+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?
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...
alias cls='printf "\033c"'
\033 == \x1B == 27 == ESC
So this becomes
<ESC>cwhich is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.
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...
clear && echo -en "\e[3J"
Or perhaps use the following alias on KDE...
alias cls='clear && echo -en "\e[3J"'
I got the scroll-back clearing command from here.
run this command:
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`?
My favorite is
printf "\ec". This can also be
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
.bashrcI have a binding like this:
bind -x '"\C-t": printf "\ec"';
Now I press ctrlt 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.
Another alternative to the @Hibou57 answer:
# The Octal Way alias clear="echo -e '\033c\c'"
# The Hex Way alias clear="echo -e '\x1b\c'"
Basically this will have exactly the same effect, it will print the
^[character (which clear your terminal) and does not output the trailing new line.