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?


Solution 1:

Yes, the command you're looking for is

reset

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:

tput reset

From the other answers:

  • 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.

Solution 2:

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.

Solution 3:

You can also assign a shortcut in gnome-terminal by going to Edit → Keyboard Shortcuts. I useShift+Ctrl+Alt+C.

reset and clear shortcut

Solution 4:

Cross posting my answer from stackoverflow.

Use the following command to do a clear screen instead of merely adding new lines ...

printf "\033c"

yes that's a 'printf' on the bash prompt.

You will probably want to define an alias though...

alias cls='printf "\033c"'

Explanation

\033 == \x1B == 27 == ESC

So this becomes <ESC>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

KDE

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.