689

I know the clear command that 'clears' the current screen, but it does this just by printing lots of newlines - the cleared contents just get scrolled up.

Is there a way to completely wipe all previous output from the terminal so that I can't reach it even by scrolling up?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eonil
  • 83,476
  • 81
  • 317
  • 516

16 Answers16

1335

To clear the terminal manually:

+K

Command+K for newer keyboards

To clear the terminal from within a shell script;

/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
  • @fearless_fool http://apple.stackexchange.com/a/31887 might do it? If it does, please let me know! – Alok Singhal Oct 28 '14 at 17:46
  • Well, yes, but see below (http://stackoverflow.com/a/26615036/558639) for a better way altogether. – fearless_fool Oct 29 '14 at 00:48
  • 9
    If you accidentally pressed this, how would one go about viewing the cleared buffer? – Joshua Pinter Nov 04 '14 at 19:41
  • 7
    @JoshPinter, just don't press it by accident. :) (Consider using `clear` for all cases *except* where you need the scrollback history to actually disappear, e.g. when you are going to print.) – Wildcard Mar 18 '16 at 07:02
  • 3
    @Wildcard Fair enough. :) Good advice on using `clear`. I feel like they should be reversed, though. Typing `clear` seems more intentional than hitting `Command + K`. – Joshua Pinter Mar 19 '16 at 13:47
  • @JoshPinter `clear` is a command that works in all the terminals (OS X's native "Terminal", X11's xterm, iTerm etc.). So it looks the environment variable `TERM` and outputs an appropriate set of characters which clear the terminal. `Command + K` only works in "Terminal". Terminal also provides a lot of commands to clear parts of the terminal (`Command + L` etc.). – Alok Singhal Mar 19 '16 at 18:34
  • @JoshPinter you can see that `clear` is doing things based on `TERM` by doing stuff like: `TERM=xterm clear | hexdump` and `TERM=tek clear | hexdump`. The output is different, so the character `clear` outputs are clearly (!) dependent on `TERM` variable. – Alok Singhal Mar 19 '16 at 18:36
  • problem is, mac OS doesn't discriminate which terminal window to clear. if the script is running in one, but your focus is in another, it'll clear the one with the focus, not the one with the script – ierdna Dec 25 '20 at 12:26
  • This doesn't play nice with tmux for some reason. It's visually clearing the tmux bar – Omer H May 09 '21 at 06:29
126

A better way to clear the screen from within a script...

If you're using the OS X Terminal app (as stated by the OP), a better approach (thanks to Chris Page's answer to How do I reset the scrollback in the terminal via a shell command?) is just this:

clear && printf '\e[3J'

or more concisely (hat tip to user qiuyi):

printf '\33c\e[3J'

which clears the scrollback buffer as well as the screen. There are other options as well. See Chris Page's answer to How do I reset the scrollback in the terminal via a shell command? for more information.

Original answer

The AppleScript answer given in this thread works, but it has the nasty side effect of clearing any terminal window that happens to be active. This is surprising if you're running the script in one window and trying to get work done in another!

You avoid this by refining the AppleScript to only clear the screen if it is frontmost by doing this (taken from MattiSG's answer to How do I reset the scrollback in the terminal via a shell command?):

osascript -e 'if application "Terminal" is frontmost then tell application "System Events" to keystroke "k" using command down'

... but as when it's not the current window, the output will stack up until it becomes current again, which probably isn't what you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • 1
    I used the "better way" in ``.bash_profile`` and it's awesome because I no longer get the glitched buffer sometimes when opening a new terminal window. – Patrick Roberts Jan 28 '15 at 09:49
  • 1
    This is the most conceptually correct answer. qiuyi's answer avoids the && at the sacrifice of a little readability. If Alok's answer could be extended to clear the terminal that is running the current script, it would be an improvement, but this simpler. – Zack Morris Dec 18 '15 at 19:41
  • Is there a way to bind a command, eg, `ctrl+l`, to do the same thing as `printf '\33c\e[3J'` using `.inputrc` ? – kortina Jul 21 '20 at 20:31
  • 1
    I could not figure out with `.inputrc` but this worked in my `.bash_profile`: `bind '"\C-k": "printf \\\\33c\\\\e[3;\n"'` – kortina Jul 21 '20 at 20:58
  • The `printf` code works in the normal terminal app, but not in the built-in terminal of PHPStorm. Anyone knows why? – Philipp Jan 12 '21 at 17:38
  • This & another answer have some overlap, and I always waste time figuring out who the original is so I'm not awarding point miners for duped work. Here, fearless has [`clear && printf '\e[3J'` from the start in 2014](https://stackoverflow.com/revisions/26615036/1). And as (maybe a little obliquely) noted, the `printf '\33c\e[3J'` improvement [is lifted from another answer](https://stackoverflow.com/a/29876027/1028230) and [repeated here in a edit](https://stackoverflow.com/revisions/26615036/5). So we've got an answer & an improvement that's duped back into the original; upvotes for everyone! – ruffin Jun 01 '21 at 23:12
  • @kortina thanks! Nice trick! Didn't know about bind with bash. – clearlight Sep 15 '21 at 15:50
77

To delete the last output only:

+ L

To clear the terminal completely:

+ K

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aman Jain
  • 2,294
  • 1
  • 16
  • 22
65

The pretty way is printf '\33c\e[3J'

qiuyi
  • 659
  • 5
  • 3
  • 10
    This is the best way. We should define alias like `alias cls='printf "\33c\e[3J"'` – mpyw Apr 27 '15 at 16:11
  • Works in iTerm2 as well – Anders Zommarin May 31 '17 at 07:09
  • 3
    @LoïcFaure-Lacroix, `\33c` performs the equivalent of the `clear` command, which basically just scrolls the screen until you can't see it's previous contents. It clears the screen, but not the scroll back buffer (i.e. you can still use the scroll bars to see the old output). Add the `\e[3J` to actually clear the scroll back buffer. – luiss Sep 19 '17 at 18:02
  • @EirNym, add `function cls { printf '\33c\e[3J\33c' }` line in `~/.profile` (or system-wide `/etc/profile`). This should work for desktop environments in macOS, FreeBSD, Linux etc. Note the extra `\33c` is for clearing the extra `\e[3J` literal in non-macOS (basically for Linux/FreeBSD, we only need `printf '\33c'`). – vulcan raven Aug 08 '19 at 16:16
  • @luiss What is the code `\33c`? Is there an official document for the full list of similar code's meaning in MacOS? – Richard Mar 15 '23 at 11:02
36

Put this in your .bash_profile or .bashrc file:

function cls {
    osascript -e 'tell application "System Events" to keystroke "k" using command down'
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert Simmons Jr.
  • 1,182
  • 8
  • 21
  • This answer still works great in Big Sur! It clears the output of the current terminal tab (other windows/tabs are not cleared) – Philipp Jan 12 '21 at 17:44
22

On Mac OS X Terminal, this functionality is already built in to the Terminal Application as menu ViewClear Scrollback (the default is CMD + K).

So you can re-assign this as you like with Apple's Keyboard shortcuts. Just add a new shortcut for Terminal with the command "Clear Scrollback". (I use CMD + L, because it's similar to Ctrl + L to clear the current screen contents, without clearing the buffer.)

I am not sure how you would use this in a script (maybe AppleScript as others have pointed out).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SeaSide
  • 229
  • 2
  • 2
  • 1
    As of Yosemite (10.10), `View->Clear Scrollback` is no longer present in Terminal's menu. The keyboard shortcut `CMD` + `K` still works, though. – Nicolas Miari Sep 28 '15 at 02:36
  • 2
    @NicolasMiari Looks like `Clear Scrollback` has just moved from `View` to `Edit` in Yosemite. – BrainSteel Nov 08 '15 at 22:03
11

With Mac OS X v10.10 (Yosemite), use Option + Command + K to clear the scrollback in Terminal.app.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David J.
  • 31,569
  • 22
  • 122
  • 174
9

Or you can send a page break (ASCII form feed) by pressing Ctrl + L.

While this technically just starts a new page, this has the same net effect as all the other methods, while being a lot faster (except for the Apple + K solution, of course).

And because this is an ASCII control command, and it works in all shells.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2897962
  • 99
  • 1
  • 1
  • 1
    That clears the screen but leaves the scrollback buffer intact. – George Jul 12 '16 at 19:39
  • it is not the same if you want to use cmd + f for exemple – bormat May 11 '19 at 10:14
  • That's not how Control-L works in a shell. Shells bind the Control-L (Form Feed) input to a command that clears the screen in some appropriate way, which is not by sending a Form Feed to the terminal. Terminals treat FF just like New Line and advance the cursor one row. Shells usually use terminfo to look up the bytes to send to the terminal. That is usually `clear=\E[H\E[2J`, which moves the cursor to the home position (ESC [ H) and erases the display (ESC [ 2 J). This is why it's important to use the `clear` command instead of hard-coding the entire sequence when doing it programmatically. – Chris Page Mar 31 '23 at 18:58
9

Command + K will clear previous output.

To clear entered text, first jump left with Command + A and then clear the text to the right of the pointer with Control + K.

Visual examples:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Punnerud
  • 7,195
  • 2
  • 54
  • 44
6
clear && printf '\e[3J'

clears out everything, and it works well on OS X as well. Very neat.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
6

Do the right thing; do the thing right!

Clear to previous mark: Command + L

Clear to previous bookmark: Option + Command + L

Clear to start: Command + K

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vittore Marcas
  • 1,007
  • 8
  • 11
0

I couldn't get any of the previous answers to work (on macOS).

A combination worked for me -

IO.write "\e[H\e[2J\e[3J"

This clears the buffer and the screen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Baldry
  • 1,990
  • 2
  • 14
  • 28
  • 1
    What is IO.write? I had to replace it with printf to get it working. Thanks anyway. This one works best for me – Fitsyu Jul 02 '18 at 10:38
  • 1
    I'm not sure why I chose `IO.write`. It should work the same however you decide to print those characters to the terminal. – Michael Baldry Jul 23 '18 at 13:07
0

Adding the following to your configuration file would get you a new command to do it.

alias clearwipe='printf "\33c\e[3J"'

After reload clearwipe would be the new command to completely wipe all previous output from the terminal so that you can't reach it even by scrolling up.

Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27
-2

This works on MacOS v13

tput reset
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • 2
    `reset` doesn't normally clear the scrollback, which is what this Q&A is about. It resets the terminal, which clears the screen. Resetting the terminal is usually undesirable. – Chris Page Mar 31 '23 at 19:06
  • 1
    mentioned command does clear all previous output as well as scrollback, that's what OP asked for. It's a different way to achieve the same. – GorvGoyl Apr 01 '23 at 07:49
-3

Typing the following in the terminal will erase your history (meaning using up arrow will get you nothing), but it will not clear the screen:

history -c
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
phil
  • 187
  • 1
  • 2
-3

CMD + K works for macOS. It clears the entire terminal output, but the environment remains.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CoeB
  • 29
  • 1