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?
When working in a Terminal window, I many times need to clear the previous output, especially if the program writes a lot of output and I need to easily tell the difference between executions, e.g. to test the most recent changes. A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.
There are several methods to clear the console or output screen and one of them is clrscr() function. It clears the screen as function invokes. It is declared in “conio.
In the Console app on your Mac, do any of the following: Clear content: Click the Clear button in the toolbar (or use the Touch Bar), or choose Action > Clear. This hides log data collected to date and only shows new messages or activities.
⌘+K
Command+K for newer keyboards
/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With