BASH 中的“CLS”等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6367046/
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
"CLS" Equivalent in BASH?
提问by user541686
How do you clear the entire terminal in BASH, like the command prompt's cls
command?
你如何在 BASH 中清除整个终端,就像命令提示符的cls
命令一样?
clear
doesn't work because it doesn't actually clear anything, it just scrolls down.
clear
不起作用,因为它实际上并没有清除任何东西,它只是向下滚动。
采纳答案by zad
You're probably looking for the reset
command.
However, the scroll-back buffer is not a feature of bash but of the terminal program. You didn't say what terminal program you were using.
但是,回滚缓冲区不是 bash 的功能,而是终端程序的功能。你没有说你使用的是什么终端程序。
回答by Srikar Appalaraju
Why don't you try Ctrl+l(control, lowercase "L"). This works in most shells (err terminals)...
为什么不试试Ctrl+ l(控制,小写“L”)。这适用于大多数外壳(错误终端)...
In OSX terminal -
在 OSX 终端中 -
Command ?+l(command, l) leads to removing last typed command from display.
Command ?+ l(command, l) 导致从显示中删除最后输入的命令。
Command ?+k(command, k) leads to removing/clearing all display buffer.
Command ?+ k(command, k) 导致删除/清除所有显示缓冲区。
reset
(type this in terminal) leads to reset of terminal in case display becomes garbled.
reset
(在终端中输入)会导致终端重置,以防显示乱码。
not sure of equivalent in other unix flavors.
不确定在其他 Unix 风格中是否等效。
回答by DouglasHeriot
As far as I know, there isn't a way to do this any better than what clear
does with bash.
据我所知,没有比clear
bash更好的方法了。
I think it's a feature that could be built into the terminal you're using though. I know the Mac Terminal app has a 'Clear Scrollback' menu option (command + k) that does what you're asking for.
我认为这是一个可以内置到您正在使用的终端中的功能。我知道 Mac 终端应用程序有一个“清除回滚”菜单选项(command + k),可以满足您的要求。
回答by Random832
xterm
will allow the escape sequence ESC [3J
to clear the scroll back, so you could do:
xterm
将允许转义序列ESC [3J
清除回滚,因此您可以执行以下操作:
alias cls="clear; printf '3[3J'"
回答by Ami
Use ?+K. It removes the entries so I can't scroll up anymore.
使用?+ K。它删除了条目,所以我不能再向上滚动了。
So ?+Kto clear everything including scrolling. Ctrl+Lto clear terminal window but still be able to see everything when scrolling up.
所以?+K清除所有内容,包括滚动。 Ctrl+L清除终端窗口,但在向上滚动时仍然可以看到所有内容。
回答by Shital Shah
Short Answer
简答
clear && clear
or
或者
tput reset
Other Ways
其他方法
Here are all the ways you can clear the terminal screen in Unix:
以下是在 Unix 中清除终端屏幕的所有方法:
clear # only clear visible screen
clear && clear # clear buffer as well
tput clear # same as clear but by sending escape seq
reset # clear + reset internal terminal state + 1sec delay
tput reset # same as reset but without 1sec delay
stty sane # don't clear screen but reset some terminal options
echo -e "3c" # same as tput reset but hardcoded escape seq
printf "3c" # same as tput reset but hardcoded escape seq
setterm -reset # same as tput reset, setterm has friendlier commands
Long Answer
长答案
The clear
command only clears the visible screen but not the buffer so you can do Shift+PageUp
to scroll up in the terminal and still view previous outputs. If you want to get same result as cls
then do clear twice like clear && clear
.
该clear
命令只清除可见屏幕而不是缓冲区,因此您可以Shift+PageUp
在终端中向上滚动并仍然查看以前的输出。如果你想得到相同的结果,cls
那么像clear && clear
.
Another related command is reset
which (I believe) resets the internal state of the terminal program. Unfortunately, this command includes 1 second of delay to support really old terminals. So if you are not ok with that kind of delay then use tput reset
which seems to do same thing as reset minus the delay.
另一个相关命令是reset
(我相信)重置终端程序的内部状态。不幸的是,此命令包含 1 秒的延迟以支持真正的旧终端。因此,如果您对这种延迟不满意,那么使用tput reset
它似乎与重置减去延迟做同样的事情。
But what does tput
do? In Unix, you can send terminal all kinds of ASCII character sequences which are interpreted as commands by the terminal. This allows you to do funky things like blink or color the text or turn off echo (during password typing) or set terminal options or do clear or reset. This you can send by tput clear
or tput reset
. The clear
and reset
command are equivalent but they run from the binaries that comes with your distro and may do additional stuff. The setterm -reset
is similar to tput reset
. Setting terminal using setterm
is usually better because unlike tput
it has more readable options in general case however we here use tput because it's smaller in length :).
但是有什么作用tput
呢?在 Unix 中,您可以向终端发送各种 ASCII 字符序列,这些字符序列被终端解释为命令。这允许您做一些时髦的事情,例如闪烁或为文本着色或关闭回显(在输入密码期间)或设置终端选项或清除或重置。这可以通过tput clear
或发送tput reset
。该clear
和reset
命令是等价的,但他们从随你的发行版,并且可以做更多的东西二进制运行。在setterm -reset
类似tput reset
。设置终端 usingsetterm
通常更好,因为tput
它在一般情况下具有更多可读选项,但是我们在这里使用 tput 因为它的长度更小:)。
You might have also seen people using things like echo -e "\033c"
or printf "\033c"
which is equivalent to tput reset
but the escape sequence is now hard coded. The tput
looks up terminal properties and uses the right escape sequence.
您可能还看到人们使用类似echo -e "\033c"
或printf "\033c"
等同于的东西,tput reset
但转义序列现在是硬编码的。在tput
查找终端的性能和用途正确的转义序列。
Another related command is stty sane
which actually doesn't do any screen clearing but it sets many of the terminal optionsto defaults so if your terminal looks garbled or if terminal stays blank when you type (for example, because you printed binary file to terminal with escape sequence to turn off echo) then this command might help. For extreme garbled terminal cases, you can use all of the available resetting techniques in the sequence. I've alias like this for such occasions:
另一个相关的命令stty sane
实际上不执行任何屏幕清除操作,但它会将许多终端选项设置为默认值,因此如果您的终端看起来乱码或键入时终端保持空白(例如,因为您使用转义将二进制文件打印到终端)顺序关闭回声)那么这个命令可能会有所帮助。对于极端乱码的终端情况,您可以使用序列中所有可用的重置技术。在这种情况下,我有这样的别名:
alias cls='tput reset'
alias clshard='reset; stty sane; tput rs1; setterm -reset; tput reset'
Related
有关的
What's the equivalent of the “cls” command from Windows/DOS?