How to 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?


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'

A better way...

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.


漂亮的方式是printf '33ce[3J'

链接地址: http://www.djcxy.com/p/49362.html

上一篇: iTerm 2:如何设置键盘快捷键跳转到开始/结束行?

下一篇: 如何在Mac OS X中清除终端中的以前输出?