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.

4
  • 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?) Mar 24, 2010 at 0:59
  • 1
    Ctrl-L will clear the screen in bash (in emacs mode, which is default), similar to executing the clear program.
    – Roger Pate
    Jun 28, 2010 at 7: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, 2014 at 10:20
  • 1
    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: superuser.com/questions/555554/…
    – jwd
    Oct 10, 2017 at 15:33

13 Answers 13

77

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'
9
  • I am using xterm. However I don't understand what you mean. Should I literately type echo ESC-c? Mar 23, 2010 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. Mar 23, 2010 at 14:49
  • 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, 2010 at 23:42
  • 3
    @HaraldNordgren its better to use the more standard printf than echo -e. Apr 11, 2016 at 17:29
  • 4
    Why \0143 instead of c?
    – ruakh
    Jul 5, 2017 at 2: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.
6
  • 20
    This does not really answer my question. Mar 23, 2010 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. Mar 23, 2010 at 15:30
  • 1
    Look up "useless use of cat"
    – galois
    Apr 10, 2018 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, 2019 at 1: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 1, 2021 at 22:09
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"'

7
  • 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.) Mar 23, 2010 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. Mar 23, 2010 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.) Mar 23, 2010 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. Mar 23, 2010 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? Mar 26, 2010 at 20:53
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.

2
  • 2
    CMD+K works in iTerm2. CTRL+K works in a lot of others.
    – blockloop
    May 10, 2014 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. Mar 22, 2016 at 21:33
10

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

3
  • Hmm, I don't have that under my gnome... Mar 26, 2010 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. Mar 26, 2010 at 21:07
  • In xterm its ctrl-middle button -> "reset and clear saved lines"
    – Craig
    Mar 26, 2010 at 23:42
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) ...
1
  • 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 3, 2020 at 9: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'
3

On Mac OS X Terminal.app:

View -> Clear Scrollback (or command-K)

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

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.

2
  • 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. Mar 23, 2010 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. Oct 4, 2021 at 17:14
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......

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .