How can I clear previous output in Terminal in Mac OS X?
Asked Answered
P

16

697

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?

Prather answered 4/2, 2010 at 9:0 Comment(4)
Did you ever find a way to do this that works in a shell script?Dangerous
@ZevEisenberg I don't think so. Anyway, now I think I don't need this at all. I am satisfied with Command+K.Prather
@ZevEisenberg my updated answer (and one other answer) shows you how to do it from a script.Oncoming
See apple.stackexchange.com/questions/31872/…Colored
O
1352

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'
Oncoming answered 4/2, 2010 at 9:4 Comment(9)
@fearless_fool apple.stackexchange.com/a/31887 might do it? If it does, please let me know!Oncoming
Well, yes, but see below (https://mcmap.net/q/63538/-how-can-i-clear-previous-output-in-terminal-in-mac-os-x) for a better way altogether.Quartermaster
If you accidentally pressed this, how would one go about viewing the cleared buffer?Pattypatulous
@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.)Lionellionello
@Lionellionello Fair enough. :) Good advice on using clear. I feel like they should be reversed, though. Typing clear seems more intentional than hitting Command + K.Pattypatulous
@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.).Oncoming
@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.Oncoming
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 scriptSidky
This doesn't play nice with tmux for some reason. It's visually clearing the tmux barTrunnel
Q
133

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.

Quartermaster answered 28/10, 2014 at 17:46 Comment(7)
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.Trenton
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.Vedic
Is there a way to bind a command, eg, ctrl+l, to do the same thing as printf '\33c\e[3J' using .inputrc ?Imperforate
I could not figure out with .inputrc but this worked in my .bash_profile: bind '"\C-k": "printf \\\\33c\\\\e[3;\n"'Imperforate
The printf code works in the normal terminal app, but not in the built-in terminal of PHPStorm. Anyone knows why?Kerosene
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. And as (maybe a little obliquely) noted, the printf '\33c\e[3J' improvement is lifted from another answer and repeated here in a edit. So we've got an answer & an improvement that's duped back into the original; upvotes for everyone!Wylma
@Imperforate thanks! Nice trick! Didn't know about bind with bash.Different
R
87

To delete the last output only:

+ L

To clear the terminal completely:

+ K

Rezzani answered 28/5, 2019 at 7:32 Comment(5)
Is there a way to clear the screen but still have the input be there if I scroll up above the fold?Canale
@Canale Under Linux clear -x keeps scrollback intact, don't have a mac at hand to test thereDarken
@Darken I just tested it out in Zsh on macOS Catalina, and it does indeed work. Thanks!Canale
For iTerm2 users, Cmd + Alt + L can delete the last line.Kiyohara
For iTerm2 users, Cmd + Alt + L can delete the last line: superuser.com/a/1767681/114723 (called Clear to Previous Mark)Kiyohara
H
67

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

Haileyhailfellowwellmet answered 26/4, 2015 at 9:43 Comment(6)
This is the best way. We should define alias like alias cls='printf "\33c\e[3J"'Perpetrate
Works in iTerm2 as wellSubtle
@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.Animadversion
@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').Arched
@Animadversion What is the code \33c? Is there an official document for the full list of similar code's meaning in MacOS?Courtmartial
Can you do a similar magic trick just to clear just the last command and its output? (equivalent of ⌘L on Mac)Suffragette
A
37

Put this in your .bash_profile or .bashrc file:

function cls {
    osascript -e 'tell application "System Events" to keystroke "k" using command down'
}
Ambrotype answered 22/2, 2013 at 7:16 Comment(1)
This answer still works great in Big Sur! It clears the output of the current terminal tab (other windows/tabs are not cleared)Kerosene
S
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).

Skill answered 14/1, 2014 at 18:0 Comment(2)
As of Yosemite (10.10), View->Clear Scrollback is no longer present in Terminal's menu. The keyboard shortcut CMD + K still works, though.Cariotta
@NicolasMiari Looks like Clear Scrollback has just moved from View to Edit in Yosemite.Shipboard
C
11

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

Carnauba answered 10/12, 2014 at 22:18 Comment(0)
D
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.

Drumfish answered 19/10, 2013 at 14:13 Comment(3)
That clears the screen but leaves the scrollback buffer intact.Infringe
it is not the same if you want to use cmd + f for exempleDispensable
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.Doyon
E
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

Editorial answered 30/1, 2019 at 20:1 Comment(0)
C
6
clear && printf '\e[3J'

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

Carrier answered 9/2, 2017 at 6:31 Comment(1)
beautiful, I was just using clear which leaves some stuff when you scroll back up. no more!Solicitude
E
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

Euphonize answered 8/2, 2019 at 6:0 Comment(0)
F
3

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.

Fawn answered 1/1, 2023 at 23:28 Comment(0)
A
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.

Areca answered 6/4, 2017 at 13:20 Comment(2)
What is IO.write? I had to replace it with printf to get it working. Thanks anyway. This one works best for meOvertly
I'm not sure why I chose IO.write. It should work the same however you decide to print those characters to the terminal.Areca
E
-2

This works on MacOS v13

tput reset
Eyebright answered 22/3, 2023 at 12:30 Comment(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.Doyon
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.Eyebright
M
-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
Miniskirt answered 25/2, 2010 at 14:46 Comment(6)
It does. @Miniskirt does it deletes them permanently from the disk, or are they still retrievable some way?Jahncke
Basically it deletes the file ~/.bash_history, so if you can recover that, you can recover the commands that have been clearedDiscomfort
No it doesn't work. Scrolled up history still exist, and I can scroll up to see them again.Prather
Not what was asked. This clears the history, not the current buffer, which are two very different things.Shearer
Not what was asked, and harmful; I was just bitten by this, and my history contained important stuff.Brouhaha
This command actually clears the scrolled up history for the ACTIVE TERMINAL ONLY. Once closed, re-opening the terminal has the history restored.Heavy
R
-3

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

Remorseful answered 2/4, 2018 at 20:46 Comment(1)
This is the same answer as the accepted one written 8 years ago.Megasporangium

© 2022 - 2024 — McMap. All rights reserved.