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'