macos 如何清除 Mac OS X 终端中的先前输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2198377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:44:13  来源:igfitidea点击:

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

macosshellbufferclear

提问by Eonil

I know the clearcommand that 'clears' the current screen, but it does this just by printing lots of newlines - the cleared contents just get scrolled up.

我知道clear“清除”当前屏幕的命令,但它只是通过打印大量换行符来实现的 - 清除的内容只是向上滚动。

Is there a way to completely wipe all previous output from the terminal so that I can't reach it even by scrolling up?

有没有办法从终端完全擦除所有以前的输出,这样即使向上滚动也无法到达它?

回答by Alok Singhal

To clear the terminal manually:

手动清除终端:

?+K

?+K

Command+Kfor newer keyboards

Command+K适用于较新的键盘

To clear the terminal from within a shell script;

从 shell 脚本中清除终端;

/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'

回答by fearless_fool

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:

如果您使用的是 OSX 终端应用程序(如 OP 所述),更好的方法(感谢https://apple.stackexchange.com/a/113168)就是这样:

clear && printf '\e[3J'

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

或者更简洁(给https://stackoverflow.com/users/4834046/qiuyi 的帽子提示):

printf 'c\e[3J'

which clears the scrollback buffer as well as the screen. There are other options as well, see https://apple.stackexchange.com/a/113168for more info.

这会清除回滚缓冲区以及屏幕。还有其他选项,请参阅https://apple.stackexchange.com/a/113168了解更多信息。

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!

该线程中给出的 AppleScript 答案有效,但它具有清除碰巧处于活动状态的任何终端窗口的令人讨厌的副作用。如果您在一个窗口中运行脚本并试图在另一个窗口中完成工作,这会令人惊讶!

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

您可以通过改进 AppleScript 来避免这种情况,仅在屏幕最前面时才清除屏幕(取自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.

...但是当它不是当前窗口时,输出将叠加直到它再次变为当前窗口,这可能不是您想要的。

回答by qiuyi

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

漂亮的方法是 printf '\33c\e[3J'

回答by Robert Simmons Jr.

Put this in your .bash_profile or .bashrc

把它放在你的 .bash_profile 或 .bashrc 中

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

回答by SeaSide

On Mac OS X Terminal this functionality is already built in to the Terminal Application as View->Clear Scrollback(Default is CMD+K).

在 Mac OS X 终端上,此功能已内置于终端应用程序中View->Clear Scrollback(默认为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+Lto clear the current screen contents, without clearing the buffer.)

因此,您可以根据需要使用 Apple 的键盘快捷键重新分配它。只需使用命令“清除回滚”为终端添加一个新的快捷方式。(我使用CMD+ L,因为它类似于CTRL+L来清除当前屏幕内容,而不清除缓冲区。)

Not sure how you would use this in a script (maybe AppleScript as others have pointed out).

不确定您将如何在脚本中使用它(也许 AppleScript 就像其他人指出的那样)。

回答by David J.

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

使用 Mac OS X Yosemite (10.10)Option + Command + K清除 Terminal.app 中的回滚。

回答by user2897962

Or you can send a page break(ASCII form feed) by pressing:

或者,您可以按以下方式发送分页符(ASCII 换页符):

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

虽然这在技术上只是开始一个新页面,但它与所有其他方法具有相同的净效果,同时速度要快得多(当然,Apple+K 解决方案除外)。

And because this is an ASCII control command, it works in all shells.

因为这是一个 ASCII 控制命令,所以它适用于所有 shell。

回答by Punnerud

Command + Kwill clear previous output

Command + K将清除以前的输出

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

要清除输入的文本,首先使用“ Command+A”向左跳,然后使用“ Control+K”清除指针右侧的文本。

Visual examples: enter image description here

视觉示例: 在此处输入图片说明

回答by Aman Jain

To delete last output only:

仅删除最后一个输出:

?+L

?+L

To clear terminal completely:

彻底清除终端:

?+K

?+K

回答by McAllister Bowman

Do right thing, do thing right!

做正确的事,做正确的事!

Clear to previous mark:Command-L

清除上一个标记:Command-L

Clear to previous bookmark:Option-Command-L

清除上一个书签:Option-Command-L

Clear to start: Command-K

清除开始: Command-K

Help your guys!

帮助你的家伙!