How to clear previous output in Terminal in Mac OS X?

点点圈 提交于 2019-11-27 16:33:39
Alok Singhal

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

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

Put this in your .bash_profile or .bashrc

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

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

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

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.

clear && printf '\e[3J'

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

Do right thing, do thing right!

Clear to previous mark:Command-L

Clear to previous bookmark:Option-Command-L

Clear to start: Command-K

Help your guys!

phil

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

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

Command + K will clear previous output

To clear entered text, first jump left with "Command+A" then clear text to the right of the pointer with "Control+K".

Visual examples:

To delete last output only:

+L

To clear terminal completely:

+K

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!