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

66

26

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.

sixtyfootersdude

Posted 2010-03-22T20:56:15.597

Reputation: 6 399

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 – 2010-03-24T00:59:01.503

1Ctrl-L will clear the screen in bash (in emacs mode, which is default), similar to executing the clear program. – None – 2010-06-28T07:59:03.763

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 – 2017-10-10T15:33:10.947

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 – 2014-05-21T10:20:19.137

Answers

57

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

Posted 2010-03-22T20:56:15.597

Reputation: 1 097

I am using xterm. However I don't understand what you mean. Should I literately type echo ESC-c? – sixtyfootersdude – 2010-03-23T13:39:42.500

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. – user1686 – 2010-03-23T14:49:11.853

Updated the answer with commands that should work. – Craig – 2010-03-23T21:02:04.333

Cool, that works awesomely! I don't understand what is going on here though. What reference should I look at? – sixtyfootersdude – 2010-03-26T20:55:47.643

1You 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 – 2010-03-26T23:42:28.443

This is great! I've been using echo -ne to avoid the trailing newline also. – Harald Nordgren – 2016-03-06T11:34:26.890

2@HaraldNordgren its better to use the more standard printf than echo -e. – Good Person – 2016-04-11T17:29:22.270

3Why \0143 instead of c? – ruakh – 2017-07-05T02:36:07.433

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 – 2012-04-01T04:09:48.647

52

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.

bignose

Posted 2010-03-22T20:56:15.597

Reputation: 1 981

14This does not really answer my question. – sixtyfootersdude – 2010-03-23T13:44:47.920

7Yeah 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 – 2010-03-23T15:30:31.397

Look up "useless use of cat" – galois – 2018-04-10T20:20:12.463

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 – 2019-03-14T01:48:18.767

24

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

Posted 2010-03-22T20:56:15.597

Reputation: 1

1I 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.) – user1686 – 2010-03-23T15:29:10.103

1For 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 – 2010-03-23T15:36:37.577

1According 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.) – user1686 – 2010-03-23T15:56:00.123

2like 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 – 2010-03-23T16:27:29.237

+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 – 2010-03-26T20:53:16.323

10

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

Posted 2010-03-22T20:56:15.597

Reputation: 201

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. – L S – 2016-03-22T21:33:23.187

1CMD+K works in iTerm2. CTRL+K works in a lot of others. – blockloop – 2014-05-10T22:08:05.103

8

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

user1686

Posted 2010-03-22T20:56:15.597

Reputation: 283 655

Hmm, I don't have that under my gnome... – sixtyfootersdude – 2010-03-26T20:54:45.913

@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. – user1686 – 2010-03-26T21:07:28.930

In xterm its ctrl-middle button -> "reset and clear saved lines" – Craig – 2010-03-26T23:42:57.180

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'

user1686

Posted 2010-03-22T20:56:15.597

Reputation: 283 655

3

On Mac OS X Terminal.app:

View -> Clear Scrollback (or command-K)

acajaja

Posted 2010-03-22T20:56:15.597

Reputation: 39

@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. – 2016-09-21T19:32:37.613

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

Steven Huang

Posted 2010-03-22T20:56:15.597

Reputation: 53

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

Posted 2010-03-22T20:56:15.597

Reputation: 11

1

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

Posted 2010-03-22T20:56:15.597

Reputation: 4 077

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

Posted 2010-03-22T20:56:15.597

Reputation: 11 754

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 – 2010-03-23T13:42:57.627

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

Posted 2010-03-22T20:56:15.597

Reputation: 161