How to clear previous output in Terminal in Mac OS

2019-01-16 00:00发布

问题:

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?

回答1:

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'


回答2:

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

If you're using the OSX Terminal app (as stated by the OP), a better approach (thanks to https://apple.stackexchange.com/a/113168) is just this:

clear && printf '\e[3J'

which clears the scrollback buffer. And it's faster than running AppleScript. There are other options as well, see https://apple.stackexchange.com/a/113168 for more info.

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 https://apple.stackexchange.com/a/31887):

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.



回答3:

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



回答4:

Put this in your .bash_profile or .bashrc

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


回答5:

On Mac OS X Terminal this functionality is already built in to the Terminal Application as View->Clear Scrollback (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.)

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



回答6:

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



回答7:

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, whilst being a lot faster (except for the Apple+K solution, of course).

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



回答8:

clear && printf '\e[3J'

clears out everything, works well on osX as well. very neat



回答9:

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

history -c


回答10:

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

A combination worked for me -

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

This clears the buffer and the screen



回答11:

CMD + K seems to work all the time for me.