我一直在使用命令:
reset
Run Code Online (Sandbox Code Playgroud)
清除我的终端。虽然我很确定这不是我应该做的。重置,顾名思义,重置你的整个终端(改变很多东西)。这是我想要的:
我基本上想使用命令clear
。但是,如果您清除然后向上滚动,您仍然会收到大量以前的内容。一般来说,这不是问题,但我正在查看很长的总日志,我想确保我只是查看最新的日志。我知道我可以使用more
或类似的东西,但我更喜欢这种方法。
Cra*_*aig 64
回滚缓冲区不是 bash 的功能,而是终端程序的功能。你没有说你使用的是什么终端。
如果您使用的是 xterm,您可以通过将 ESC-c 回显到终端来清除保存的行。
这可能适用于您使用的任何终端程序,也可能不起作用。
在 linux 上,这可能会起作用:
echo -e '\0033\0143'
Run Code Online (Sandbox Code Playgroud)
在 FreeBSD 上 echo 不接受 -e,因此您可以尝试:
printf '\033\143'
Run Code Online (Sandbox Code Playgroud)
big*_*ose 60
为每项工作使用正确的工具:
使用clear
清除终端窗口。
用于reset
在终端被控制序列搞砸时重置终端。
cat
仅当您希望将大量数据不间断地从一个地方传输到另一个地方时才使用。
使用诸如less
或 之类的寻呼程序most
来查看输出页面。
使用tail -f /var/log/foo.log /var/log/bar.log
看几个不同的日志文件。
tail
,该-F
选项更好,因为即使在其位置出现新文件时,它也可以继续跟踪文件,这在日志文件中很常见。Eva*_*oll 27
只是为了提供技术答案:reset
重新初始化终端,就好像它从头开始重新打开一样。stty sane
将执行许多相同的功能(无需重置)。这与^L
(Ctrl+L) (irrc) 和tput clear
. 尽管之前的海报 (@grawity) 说了些什么,clear
但不会输出一堆换行符。它发送或 中TERM
定义的重置,对我来说,使用(xterm) 它与命令相同。terminfo
termcap
gnome-terminal
perl -e'print "\33[H\33[2J"'
如果您只想清除缓冲区 - 与重置整个终端相比,请尝试此操作tput reset
。它应该非常快,并且做你想做的。(虽然你真的应该用 阅读文件less
)
tput reset
, 发送用于重置的 terminfo 值——在我的终端 (xterm) 上它与 perl -e'print "\33c"'
Sam*_*son 10
另一个终端是 iTerm2,它有一个有点奇怪的转义序列用于清除回滚。在 Bash shell 中,我使用类似的东西:
echo -ne '\033]50;ClearScrollback\a'
Run Code Online (Sandbox Code Playgroud)
在脚本中。所以基本上它是一个 ESC 字符,后跟“ ]50;ClearScrollback
”,然后是一个 BEL 字符。
清除所有内容的最佳方法可能是使用终端的功能:
这样,两个缓冲区都被清除干净,并且终端状态被重置为启动时的状态(这可能与使用相同,也可能不同reset
)。
要在运行 PuTTY 时清除控制台屏幕和回滚缓冲区,这对我有用:
echo -en "\ec\e[3J"
Run Code Online (Sandbox Code Playgroud)
这实际上是 2 个独立作用的“Esc”序列...它们可以按任意顺序使用:
# clears the console screen, but not the scrollback buffer
# this is actually the escape code to "reset" the terminal
echo -en "\ec"
# clears the scrollback buffer, but not the console screen
# screen content remains, and cursor position remains at its last position
echo -en "\e[3J"
Run Code Online (Sandbox Code Playgroud)
使用echo -en "\ec"
它重置终端可能会更改您的一些其他终端设置。您可以这样做,而不是“重置”:
# position the cursor to "Home" (Top Row, First Column)
echo -en "\e[H"
# Erase down: clear the screen from the cursor down to the bottom of the screen.
echo -en "\e[J"
# Note: this is supposed to clear the screen and position the cursor to home,
# but it didn't work like that for me. It cleared the entire screen (above and
# below the cursor), but left the cursor at its last position.
echo -en "\e[2J"
# putting everything together
echo -en "\e[H\e[J\e[3J"
Run Code Online (Sandbox Code Playgroud)
您可以将其放入 shell 脚本中,它就可以正常工作。
如果存在一些系统依赖性:
我正在使用 PuTTY 连接管理器(版本 0.7.1 BETA(内部版本 136))和 PuTTY(版本 0.60)。
打字:
echo \"$TERM\"; /bin/sh --version
Run Code Online (Sandbox Code Playgroud)
报告:
"xterm"
GNU bash, version 4.1.2(1)-release-(x86_64-redhat-linux-gnu) ...
Run Code Online (Sandbox Code Playgroud)