What commands can I use to reset and clear my terminal?

I have been using the command:

reset

to clear my terminal. Although I am pretty sure this is not what I should be doing. Reset, as the name suggests, resets your entire terminal (changes lots of stuff). Here is what I want:

I basically want to use the command clear. However, if you clear and then scroll up you still get tonnes of stuff from before. In general this is not a problem, but I am looking at gross logs that are long and I want to make sure that I am just viewing the most recent one. I know that I could use more or something like that but I prefer this approach.


Solution 1:

The scrollback buffer is not a feature of bash but of the terminal program. You didn't say what terminal you using.

If you are using xterm you might be able to clear the saved lines by echoing ESC-c to the terminal.

This may or may not work on whatever terminal program you are using.

On linux this will probably work:

echo -e '\0033\0143'

on FreeBSD echo doesn't accept -e so you can try:

printf '\033\143'

Solution 2:

Use the right tool for each job:

  • Use clear to clear the terminal window.

  • Use reset to reset your terminal when it gets messed up by control sequences.

  • Use cat only when you want to stream a whole lot of data from one place to another uninterrupted.

  • Use head to stream just the first few (choose how many, with -n) lines of text output.

  • Use a pager program such as less or most to view pages of output.

  • Use tail -f /var/log/foo.log /var/log/bar.log to watch several different log files.

    • With GNU tail, the -F option is better because it can continue following the file even when a new file appears in its place, as is common for log files.

Solution 3:

Just to provide the technical answer: reset reinitialize the terminal, as if it was reopened from scratch. stty sane will do a lot of the same functionality (without the reset). This is the same thing as ^L (Ctrl+L) (irrc), and tput clear. Despite what the previous poster (@grawity) said, clear does not output a bunch of newlines. It sends the TERM's reset as defined in terminfo or termcap, for me, using gnome-terminal (xterm) it is the same as the command perl -e'print "\33[H\33[2J"'.

If you want to just clear the buffer -- as compared to reseting the whole terminal, try this tput reset. It should be very fast, and do what you want. (Though you really should be reading files with less)

tput reset, sends the terminfo value for reset -- on my terminal (xterm) it is the same as perl -e'print "\33c"'

Solution 4:

Another terminal is iTerm2, and it's got a somewhat strange escape sequence used to clear scrollback. In a Bash shell, I use something like:

echo -ne '\033]50;ClearScrollback\a'

in a script. So basically it's an ESC character, followed by "]50;ClearScrollback" and then a BEL character.

Solution 5:

Probably the best way of clearing everything is to use the terminal's function:

  • Konsole: Ctrl+Shift+K View → Clear Scrollback and Reset
  • GNOME Terminal: Edit → Reset and Clear
  • PuTTY: Ctrl+right-click → Clear Scrollback

This way both buffers are wiped clean, and the terminal state is reset to exactly what it was on startup (which may or may not be the same as using reset).