107

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.

Toby Speight
  • 5,213

13 Answers13

82

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'
Craig
  • 1,347
71

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.
bignose
  • 3,325
36

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"'

Evan Carroll
  • 9,518
13

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.

Sam Mason
  • 329
11

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

grawity
  • 501,077
6

To clear the console screen and the scrollback buffer when running PuTTY, this works for me:

echo -en "\ec\e[3J"

This is actually 2 "Esc" sequences that act independently... they can be used in either order:

# clears the console screen, but not the scrollback buffer
# this is actually the escape code to "reset" the terminal
echo -en "\ec"

# clears the scrollback buffer, but not the console screen
# screen content remains, and cursor position remains at its last position
echo -en "\e[3J"

Using echo -en "\ec" which resets the terminal might change some of your other terminal settings. Instead of "Reset", you could do this:

# position the cursor to "Home" (Top Row, First Column)
echo -en "\e[H"

# Erase down: clear the screen from the cursor down to the bottom of the screen.
echo -en "\e[J"

# Note: this is supposed to clear the screen and position the cursor to home,
# but it didn't work like that for me. It cleared the entire screen (above and 
# below the cursor), but left the cursor at its last position.
echo -en "\e[2J"

# putting everything together
echo -en "\e[H\e[J\e[3J"

You can put this in a shell script and it works just fine.


In case there are some system dependencies:

I'm using PuTTY Connection Manager (Version 0.7.1 BETA (build 136)), with PuTTY (Release 0.60).

Typing:

echo \"$TERM\"; /bin/sh --version

reports:

"xterm"
GNU bash, version 4.1.2(1)-release-(x86_64-redhat-linux-gnu) ...
Kevin Fegan
  • 4,997
5

On Mac OS X Terminal.app:

View -> Clear Scrollback (or command-K)

Renan
  • 8,062
acajaja
  • 59
  • 1
  • 1
3
less -W +F foo.log

+F is for "follow", similar to tail -f but lets you scroll back too.

All vte-based terminals (GNOME's, Xfce's, Roxterm) and KDE Konsole let you use the scroll wheel to scroll inside less. I find that quite convienent.


Alternative to clear:

perl -e 'print "\n"x512;'

xterm -e 'tail -f foo.log'
grawity
  • 501,077
2

It is not a "Bash" problem. It depends on the terminal you use. For example, i use "iterm2" with macbook to connect a remote linux machine. You can use "command + K" to clean the buffer, or in the menu, choose "Edit"->"Clear Buffer".

1

To clean screen buffer for hardware TTY in FreeBSD you can use "vidcontrol -- system console control and configuration utility" with parameter -C Clear the history buffer.

vidcontrol -C

the command will blank out all screen buffer for the current console, above what you see at the moment. You might want to 'clear' first, or not - up to you.

user
  • 11
0

If you want to be sure you're looking at the most recent entries in a log file, it's probably best to use tail instead of clear / cat which I assume you're using.

Josh K
  • 12,990
0

I had this problem, after installing anaconda my terminal cursor starts to jump up every time I do clear, and I was executing reset in order to see the cursor again. Finally I ended up in doing the following:

in vi ~/.bashrc:

alias clear="echo -e '\0033\0143'"

0

Hmmm. I guess if you're running konsole, you're out of luck. It used to be you could just "clear scrollback". Konsole won't let you do that any more. You gotta reset it, too, so it kills any program you were running. I guess I need a new terminal program......

Bruce
  • 191