真正清除终端屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5367068/
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
Clear a terminal screen for real
提问by Autodidact
Using the clear
command on the terminal only fools the user into thinking the screen has been cleared...you can still see output from the previous commands when you scroll using the mouse. This makes life difficult when you are drowning in a tsunami of text.
clear
在终端上使用命令只会让用户误以为屏幕已被清除……当您使用鼠标滚动时,您仍然可以看到先前命令的输出。当你淹没在文本海啸中时,这会让你的生活变得困难。
Various solutions (escape code etc.) which can be found on the Internet are only variations of what the clear command already does.
可以在 Internet 上找到的各种解决方案(转义码等)只是 clear 命令已经执行的功能的变体。
So how do you clear the contents of a terminal in Linux for real?
那么如何在 Linux 中真正清除终端的内容呢?
采纳答案by Autodidact
Use the following command to do a clear screen instead of merely adding new lines ...
使用以下命令清除屏幕,而不仅仅是添加新行...
printf "3c"
yes that's a 'printf' on the bash prompt.
是的,这是 bash 提示符下的“printf”。
You will probably want to define an alias though...
不过,您可能想要定义别名...
alias cls='printf "3c"'
Explanation
解释
3 == \x1B == 27 == ESC
So this becomes <ESC>c
which is the VT100 escape code for resetting the terminal. Hereis some more information on terminal escape codes.
所以这变成<ESC>c
了用于重置终端的 VT100 转义码。以下是有关终端转义码的更多信息。
Edit
编辑
Here are a few other ways of doing it...
这里有一些其他的方法来做到这一点......
printf "\ec" #\e is ESC in bash
echo -en "\ec" #thanks @Jonathon Reinhart.
# -e Enable interpretation of of backslash escapes
# -n Do not output a new line
KDE
凯德
The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer...
以上在 KDE 控制台(称为 Konsole)上不起作用,但有希望!使用以下命令序列清除屏幕和回滚缓冲区...
clear && echo -en "\e[3J"
Or perhaps use the following alias on KDE...
或者也许在 KDE 上使用以下别名...
alias cls='clear && echo -en "\e[3J"'
I got the scroll-back clearing command from here.
我从这里得到了回滚清除命令。
回答by vpit3833
Try reset
. It clears up the terminal screen but the previous commands can be accessed through arrow or whichever key binding you have.
试试reset
。它清除终端屏幕,但可以通过箭头或您拥有的任何键绑定访问先前的命令。
回答by Max
Compile this app.
编译这个应用程序。
#include <iostream>
#include <cstring>
int main()
{
char str[1000];
memset(str, '\n', 999);
str[999] = 0;
std::cout << str << std::endl;
return 0;
}
回答by The111
With KDE and Ubuntu 12.04 LTS and the "Konsole" terminal, none of the posted answers work. However, pressing default keyboard shortcut CTRL+Shift+X does work! Source:
使用 KDE 和 Ubuntu 12.04 LTS 以及“Konsole”终端,发布的答案都不起作用。但是,按默认键盘快捷键 CTRL+Shift+X 确实有效!来源:
回答by N1mr0d
The following link will explain how to make that alias permanent so you don't have to keep typing it in.
以下链接将解释如何使该别名永久化,以便您不必继续输入。
https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias
https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias
These are the steps detailed at that link.
这些是该链接中详述的步骤。
- vim ~/.bashrc or gedit ~/.bashrc or what ever text editor you like
- put alias cls='printf "\033c"' at the bottom of the file
- save and exit
- . ~/.bashrc (and yes there should be a space between . and ~)
- now check to see if everything worked!
- vim ~/.bashrc 或 gedit ~/.bashrc 或任何你喜欢的文本编辑器
- 将 alias cls='printf "\033c"' 放在文件底部
- 保存并退出
- . ~/.bashrc (是的,. 和 ~ 之间应该有一个空格)
- 现在检查是否一切正常!
I take no credit for this information just passing it along.
我不相信这些信息只是传递它。
回答by TecBrat
None of the answers I read worked in PuTTY, so I found a comment on this article:
我读过的所有答案都不适用于 PuTTY,所以我在这篇文章中找到了一条评论:
In the settings for your connection, under "Window->Behavior" you'll find a setting "System Menu Appears on ALT alone". Then CTRL+L,ALT,l (that's a lower case L) will scroll the screen and then clear the scrollback buffer.
在您的连接设置中,在“窗口->行为”下,您会找到一个设置“系统菜单仅出现在 ALT 上”。然后 CTRL+L,ALT,l(这是一个小写的 L)将滚动屏幕,然后清除回滚缓冲区。
(relevant to the OP because I am connecting to an Ubuntu server, but also apparently relevant no matter what your server is running.)
(与 OP 相关,因为我连接到 Ubuntu 服务器,但无论您的服务器正在运行什么,显然也相关。)
回答by TecBrat
tput reset
That will do the trick!
这样就行了!
回答by elbedoit
My favourite human friendly command for this is:
我最喜欢的人性化命令是:
reset
Tested on xterm and VT100. It also helps after an abnormal program termination. Keeps the command buffer, so up-arrow will cycle through previous commands.
在 xterm 和 VT100 上测试。它在程序异常终止后也有帮助。保留命令缓冲区,因此向上箭头将循环浏览以前的命令。
cheers :D
欢呼:D
回答by Nithin
echo -e "\e[3J"
This works in Linux Machines
这适用于 Linux 机器
回答by nemesisfixx
I know the solution employing printing of new lines isn't much supported, but if all else fails, why not? Especially where one is operating in an environment where someone else is likely to be able to see the screen, yet not able to keylog. One potential solution then, is the following alias:
我知道采用打印新行的解决方案不太受支持,但如果所有其他方法都失败了,为什么不呢?特别是当一个人在其他人可能能够看到屏幕但无法进行键盘记录的环境中操作时。那么一种潜在的解决方案是以下别名:
alias c="printf '\r\n%.0s' {1..50}"
Then, to "clear" away the current contents of the screen (or rather, hide them), just type c+Enter
at the terminal.
然后,要“清除”屏幕的当前内容(或者更确切地说,隐藏它们),只需c+Enter
在终端输入即可。