Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
there seems to be a lot of confusion on what exactly is being asked here. can you clarify the question at all? what exactly are you trying to accomplish, clearing your terminal scrollback buffer? (if so, what terminal application are you using?) –  quack quixote Mar 24 '10 at 0:59
    
Ctrl-L will clear the screen in bash (in emacs mode, which is default), similar to executing the clear program. –  Roger Pate Jun 28 '10 at 7:59
add comment

10 Answers

up vote 19 down vote accepted

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'
share|improve this answer
    
I am using xterm. However I don't understand what you mean. Should I literately type echo ESC-c? –  sixtyfootersdude Mar 23 '10 at 13:39
2  
@sixtyfootersdude: No, not a literal ESC. Read the articles "Escape character" and "Control character" on Wikipedia -- ESC is often used to denote ASCII character 0x1B, which you can insert by pressing ^V followed by Esc. –  grawity Mar 23 '10 at 14:49
    
Updated the answer with commands that should work. –  Craig Mar 23 '10 at 21:02
    
Cool, that works awesomely! I don't understand what is going on here though. What reference should I look at? –  sixtyfootersdude Mar 26 '10 at 20:55
    
You shouldn't. If your solution involves writing raw terminal escape sequences you should rethink the solution. I would use "less" instead of cat. If you really want learn about "terminal escape sequences" Google can help you. –  Craig Mar 26 '10 at 23:42
show 1 more comment

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 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.
share|improve this answer
1  
+1 for using the right tool for the job. –  Dennis Williamson Mar 23 '10 at 12:49
3  
This does not really answer my question. –  sixtyfootersdude Mar 23 '10 at 13:44
5  
Yeah it does. Don't cat files to read them, cat files to concatenate them. Use less or more to read files - then you don't have the same problem. –  Rich Bradshaw Mar 23 '10 at 15:30
add comment

Just to to go 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"'

share|improve this answer
    
I did not say clear outputs a bunch of newlines. I just provided a command as an alternative to it, because clear only clears the screen but not the scrollback buffer. (Not in PuTTY or Konsole, at least.) –  grawity Mar 23 '10 at 15:29
    
For just that reason, printing a bunch of newlines is not really an alternative to clear -- you have no idea of what the terminal will do with the clear signal. It is better to just tell new people you have a database that knows how to handle your terminal, it has two key-values one that maps clear as a signal name to a signal, and one that maps reset to a signal -- and only the latter results in a cleared scroll back buffer. reset sends the latter, above doing other fun stuff. –  Evan Carroll Mar 23 '10 at 15:36
1  
According to sixtyfootersdude's original question, clear does not reset his scrollback. (ESC c works in xterm and VTE-based, but not in PuTTY and Konsole.) He doesn't want to use reset either, for it resets more than he wants. (I don't know which terminal he uses - but compare stty before and after resetting.) –  grawity Mar 23 '10 at 15:56
    
like I said, he probably wants tput reset, which sends the reset signal to the terminal -- without actually reinitializing the term. "reset -- Instead of putting out initialization strings, the terminal's reset strings will be output if present (rs1, rs2, rs3, rf). If the reset strings are not present, but initialization strings are, the initialization strings will be output. Otherwise, reset acts identically to init." Here, we have reset strings so the init strings aren't run. –  Evan Carroll Mar 23 '10 at 16:27
    
+1: for the suggestion, however tput reset is quite slow (same as reset). printf '\033\143' is much faster. Is there any reason why the printf method is dangerous/bad? –  sixtyfootersdude Mar 26 '10 at 20:53
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
Hmm, I don't have that under my gnome... –  sixtyfootersdude Mar 26 '10 at 20:54
    
@sixtyfootersdude: I'm sure it is there, I just don't know the exact menu (can't use gnome-terminal myself at the moment)... Take a look at "Tools" and other menus. –  grawity Mar 26 '10 at 21:07
    
In xterm its ctrl-middle button -> "reset and clear saved lines" –  Craig Mar 26 '10 at 23:42
    
thanks for tip of Putty –  kev May 25 '11 at 5:23
add comment
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'
share|improve this answer
add comment

On Mac OS X Terminal.app:

View -> Clear Scrollback (or command-K)

share|improve this answer
add comment

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.

share|improve this answer
    
Actually I am using tail -f (continuous logs) however because they occasionally spit out thousands of lines it is important to have scroll back. However it would be nice to know that I am not scowling back to an older log. –  sixtyfootersdude Mar 23 '10 at 13:42
add comment

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

share|improve this answer
add comment

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) ...
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.