eonil
eonil

Reputation: 86125

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

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?

Upvotes: 704

Views: 375984

Answers (16)

GorvGoyl
GorvGoyl

Reputation: 49570

This works on MacOS v13

tput reset

Upvotes: -1

Yonas Kassa
Yonas Kassa

Reputation: 3720

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.

Upvotes: 5

Aman Jain
Aman Jain

Reputation: 2602

To delete the last output only:

+ L

To clear the terminal completely:

+ K

Upvotes: 92

Vittore Marcas
Vittore Marcas

Reputation: 1215

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

Upvotes: 6

Punnerud
Punnerud

Reputation: 8071

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

Upvotes: 9

fearless_fool
fearless_fool

Reputation: 35239

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.

Upvotes: 137

CoeB
CoeB

Reputation: 29

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

Upvotes: -3

Michael Baldry
Michael Baldry

Reputation: 2028

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.

Upvotes: 0

jeevjyot singh chhabda
jeevjyot singh chhabda

Reputation: 637

clear && printf '\e[3J'

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

Upvotes: 6

David J.
David J.

Reputation: 32715

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

Upvotes: 11

SeaSide
SeaSide

Reputation: 229

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

Upvotes: 22

user2897962
user2897962

Reputation: 99

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.

Upvotes: 9

Robert Simmons Jr.
Robert Simmons Jr.

Reputation: 1182

Put this in your .bash_profile or .bashrc file:

function cls {
    osascript -e 'tell application "System Events" to keystroke "k" using command down'
}

Upvotes: 37

phil
phil

Reputation: 187

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

Upvotes: -3

Alok Singhal
Alok Singhal

Reputation: 96221

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'

Upvotes: 1361

qiuyi
qiuyi

Reputation: 689

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

Upvotes: 68

Related Questions