102

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
  • 4,967
  • 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 00:59
  • 1
    Ctrl-L will clear the screen in bash (in emacs mode, which is default), similar to executing the clear program. –  Jun 28 '10 at 07:59
  • Just tested this in the bash terminal in IntelliJ IDE. Works fine and clears the scrollback which is exactly what I wanted. Thanks. – Ian Lewis May 21 '14 at 10:20
  • 2
    I know this has been answered to death, but I think what you want is clear && printf '\033[3J'. No terminal resetting, just clearing the text on the screen. See this post: https://superuser.com/questions/555554/putty-clear-scrollback-from-commandline – jwd Oct 10 '17 at 15:33

13 Answers13

78

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,307
  • 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. – u1686_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
  • 1
    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
  • When typing ^v, Esc, c on my terminal (Guake or Gnome terminal), the screen is cleared, but I get the error command not found. That's a minor point, but now when I try history, the terminal interprets the "^V, Esc, c" literally, clearing the screen again. To fix this, edit ~/.bash_history , and remove the offending line. On my terminal, echo -e '\0033\0143' works without any issues. – Sparhawk Apr 01 '12 at 04:09
  • This is great! I've been using echo -ne to avoid the trailing newline also. – Harald Nordgren Mar 06 '16 at 11:34
  • 3
    @HaraldNordgren its better to use the more standard printf than echo -e. – Good Person Apr 11 '16 at 17:29
  • 5
    Why \0143 instead of c? – ruakh Jul 05 '17 at 02:36
68

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,127
  • 20
    This does not really answer my question. – sixtyfootersdude Mar 23 '10 at 13:44
  • 8
    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
  • 1
    Look up "useless use of cat" – galois Apr 10 '18 at 20:20
  • 1
    Many people are opposed to the use of cat to read anything, but for small files it actually makes a lot of sense to cat them rather than using less. Once the file has been dumped out to the terminal, you can look/scroll back to the file as often as you want, rather than having to dismiss less before you do anything else. – GKFX Mar 14 '19 at 01:48
  • ...unless it's actually a huge file which will spew pages and pages of output over a potentially slow remote connection as you try to stop it. Maybe this answer should include "Use head for quickly viewing small files." – Phil Frost Jun 01 '21 at 22:09
  • @GKFX Have you tried the less flag -F/--quit-if-one-screen? – Franklin Yu Jan 05 '22 at 06:07
33

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
  • 8,895
  • 1
    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.) – u1686_grawity Mar 23 '10 at 15:29
  • 1
    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.) – u1686_grawity Mar 23 '10 at 15:56
  • 3
    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
  • 2
    +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
  • 1
    @sixtyfootersdude The print method assumes that every terminal accepts that escape code for reset. That may be true, but it's probably not. How's that for the longest delay in answering a follow-up. – Evan Carroll May 16 '22 at 21:40
  • It turns out that Konsole version 21.12.3 doesn't clear scrollback buffer with tput reset but instead just clears the current screen. – Mikko Rantalainen Feb 13 '24 at 15:05
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
  • 309
  • 2
    CMD+K works in iTerm2. CTRL+K works in a lot of others. – blockloop May 10 '14 at 22:08
  • I like this solution because it let me clear the iTerm2 scrollback buffer from a shell script, when things like clear and reset would not. This is not really the best answer to the question, though. I think the right answer for that case is whatever the terminal application offers, like command-K in iTerm2, control-K in others, etc. – Mr. Lance E Sloan Mar 22 '16 at 21:33
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).

u1686_grawity
  • 452,512
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,887
  • This is much better answer then main answer. Actually this is the only answer that really clears scrollback buffer, all others just clear screen. I don't need fancy command to clear screen, I can do that with Ctrl-L. – blur Nov 03 '20 at 09:14
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'
u1686_grawity
  • 452,512
3

On Mac OS X Terminal.app:

View -> Clear Scrollback (or command-K)

Renan
  • 8,011
acajaja
  • 39
  • 1
  • @Rohit If the question was specifically about Mac OS X Terminal.app, then, yes, this is a great answer. However, the original question is ambiguous, best I can tell, with regards to which terminal and OS are in play. – David J. Sep 21 '16 at 19:32
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,770
  • 1
    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
  • Another case like this: when g++ emits thousands of errors, then you fix something in your .cpp, then recompile and get another thousand errors, it would be nice to find the start of your latest compile. Doing that with the scrollbar thumb is tedious. – Camille Goudeseune Oct 04 '21 at 17:14
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