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

末鹿安然 提交于 2019-11-26 18:42:28

问题


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'

or more concisely (hat tip to https://stackoverflow.com/users/4834046/qiuyi):

printf '\33c\e[3J'

which clears the scrollback buffer as well as the screen. 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:


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:




回答10:


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!




回答11:


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



回答12:


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




回答13:


To delete last output only:

+L

To clear terminal completely:

+K




回答14:


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



来源:https://stackoverflow.com/questions/2198377/how-to-clear-previous-output-in-terminal-in-mac-os-x

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