真正清除终端屏幕

发布于 2024-10-23 13:36:41 字数 1549 浏览 2 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

像你 2024-10-30 13:36:41

使用以下命令进行清屏,而不仅仅是添加新行...

printf "\033c"

是的,这是 bash 提示符上的“printf”。

不过,您可能想要定义一个别名...

alias cls='printf "\033c"'

说明

\033 == \x1B == 27 == ESC

因此,这将变为 c,它是用于重置终端的 VT100 转义码。 这里是一些有关终端转义码的更多信息。

编辑

这里有一些其他的方法...

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

上面的方法在 KDE 控制台(称为 Konsole)上不起作用,但还是有希望的!使用以下命令序列来清除屏幕和向后滚动缓冲区...

clear && echo -en "\e[3J"

或者也许在 KDE 上使用以下别名...

alias cls='clear && echo -en "\e[3J"'

我从 这里

Use the following command to do a clear screen instead of merely adding new lines ...

printf "\033c"

yes that's a 'printf' on the bash prompt.

You will probably want to define an alias though...

alias cls='printf "\033c"'

Explanation

\033 == \x1B == 27 == ESC

So this becomes <ESC>c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.

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

clear && echo -en "\e[3J"

Or perhaps use the following alias on KDE...

alias cls='clear && echo -en "\e[3J"'

I got the scroll-back clearing command from here.

楠木可依 2024-10-30 13:36:41

尝试重置。它会清除终端屏幕,但可以通过箭头或您拥有的任何键绑定来访问之前的命令。

Try reset. It clears up the terminal screen but the previous commands can be accessed through arrow or whichever key binding you have.

虫児飞 2024-10-30 13:36:41
tput reset

这样就可以了!

tput reset

That will do the trick!

橙味迷妹 2024-10-30 13:36:41

我读到的答案在 PuTTY 中都不起作用,所以我在 本文

在连接设置中的“窗口 -> 行为”下,您会找到一个设置“系统菜单出现在 ALT 上”独自的”。然后CTRL + LALTl(这是一个小写的L)将滚动屏幕,然后清除回滚缓冲区。

(与OP相关,因为我正在连接到Ubuntu服务器,但无论您的服务器运行什么,显然也相关。)

None of the answers I read worked in PuTTY, so I found a comment on this article:

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.

(relevant to the OP because I am connecting to an Ubuntu server, but also apparently relevant no matter what your server is running.)

随遇而安 2024-10-30 13:36:41
  1. 清洁可见屏幕

    <前><代码>清除

  2. 清洁屏幕并清除缓冲区

     清除 &&清除 
    
  3. 清洁并延迟 1 秒

    <预置><代码>重置

  4. 干净且无 1 秒延迟

     tput 重置
    
  1. Clean the visible screen

     clear 
    
  2. Clean screen and clear buffer

     clear && clear 
    
  3. Clean and 1-sec delay

     reset
    
  4. Clean without 1-sec delay

     tput reset
    
哑剧 2024-10-30 13:36:41

我最喜欢的人类友好命令是:

reset

在 xterm 和 VT100 上测试。它在程序异常终止后也有帮助。
保留命令缓冲区,因此向上箭头将循环显示先前的命令。

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

单调的奢华 2024-10-30 13:36:41

以下链接将解释如何使该别名永久存在,这样您就不必继续输入它。

https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias

这些是该链接中详细说明的步骤。

  1. vim ~/.bashrcgedit ~/.bashrc 或您喜欢的任何文本编辑器
  2. alias cls='printf "\033c"'在文件底部
  3. 保存并退出
  4. 。 ~/.bashrc (是的,.~ 之间应该有一个空格)
  5. 现在检查一切是否正常!

我不相信这些信息只是传递它。

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

These are the steps detailed at that link.

  1. vim ~/.bashrc or gedit ~/.bashrc or what ever text editor you like
  2. put alias cls='printf "\033c"' at the bottom of the file
  3. save and exit
  4. . ~/.bashrc (and yes there should be a space between . and ~)
  5. now check to see if everything worked!

I take no credit for this information just passing it along.

我最亲爱的 2024-10-30 13:36:41

我知道采用打印新行的解决方案并没有得到太多支持,但如果其他一切都失败了,为什么不呢?特别是当一个人在其他人可能能够看到屏幕但无法进行键盘记录的环境中操作时。那么,一种可能的解决方案是使用以下别名:

alias c="printf '\r\n%.0s' {1..50}"

然后,要“清除”屏幕的当前内容(或者更确切地说,隐藏它们),只需在终端键入 c+Enter 即可。

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.

浅紫色的梦幻 2024-10-30 13:36:41

只需补充一下,tmux 滚动缓冲区不会通过 clearresetprintf 清除。您需要:clear-history。请参阅链接

Just to add that tmux scroll buffer does not clear with clear, reset or printf. You need to :clear-history. See link.

旧人九事 2024-10-30 13:36:41

对于 KDE 和 Ubuntu 12.04 LTS 以及“Konsole”终端,发布的答案都不起作用。但是,按默认键盘快捷键 CTRL+Shift+X 确实有效!来源:

https://bugs.kde.org/show_bug.cgi?id=288913

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:

https://bugs.kde.org/show_bug.cgi?id=288913

烟酉 2024-10-30 13:36:41
echo -e "\e[3J"

这适用于 Linux 机器

echo -e "\e[3J"

This works in Linux Machines

云归处 2024-10-30 13:36:41

编译这个应用程序。

#include <iostream>
#include <cstring>

int main()
{
  char str[1000];
  memset(str, '\n', 999);
  str[999] = 0;
  std::cout << str << std::endl;
  return 0;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文