bash 如何将终端回滚到给出最后一个命令的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17212643/
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
How to scroll back the terminal to where the last command was given?
提问by kramer65
I've got a program which has a lotof output. Once it's done I often want to scroll back to the begin of the run so that I can look at some things there. Since the output is so long though, I see myself endlessly scrolling with PageUp and trying to drag the scrollbar on the right to the point where it could have begun. Over a while this starts getting quite tiresome, so I wonder:
我有一个有很多输出的程序。完成后,我经常想回滚到运行的开始处,以便我可以查看那里的一些内容。由于输出太长,我看到自己无休止地使用 PageUp 滚动并试图将右侧的滚动条拖到它可能开始的位置。过了一段时间这开始变得很烦人,所以我想知道:
Is there a way to easily have the terminal scroll back to the part where the last command was given?
有没有办法让终端轻松地滚动回给出最后一个命令的部分?
回答by Skylark
I have just found this in Terminal for OSX:
我刚刚在 OSX 终端中找到了这个:
Edit > Navigate > Jump to Previous Mark: cmd + UP.
编辑 > 导航 > 跳转到上一个标记:cmd + UP。
回答by sirius
I use iTerm2in macOS, I came up with a method. First, you should make sure you check the Unlimited scrollbackin iTerm2's preferences.

我iTerm2在macOS中使用,我想出了一个方法。首先,您应该确保检查Unlimited scrollbackiTerm2 中的首选项。

After you run a command in terminal and got a long long output.
在终端中运行命令并得到长长的输出后。
Press Cmd+F(maybe ctrl+Fin windows) then your can search in terminal like this:
按Cmd+ F(在 Windows 中可能是ctrl+ F)然后您可以在终端中搜索,如下所示:
Finally, just search your user name and press Enter, generally speaking you will jump to the last command:
最后,只要搜索你的用户名并按Enter,一般来说你会跳到最后一个命令:
回答by l0b0
If you're using a terminal like GNOME Terminal, you can search backwards. For example, Ctrl+Shift+fthen enter either the literal command or a regular expression to match it (and make sure "Match as regular expression" is set accordingly).
如果您使用像 GNOME 终端这样的终端,则可以向后搜索。例如,Ctrl+ Shift+f然后输入任一字面命令或正则表达式匹配它(并确保“匹配为正则表达式”相应地设置)。
A workaround would be to send the output to a pager such as less, where you can navigate and inspect the output, then return to the command line as if nothing had been printed.
一种解决方法是将输出发送到诸如 的寻呼机less,您可以在其中导航和检查输出,然后返回到命令行,就好像没有打印任何内容一样。
回答by pasbi
A simple Hack which works if it takes some time until most of the output is printed (like when you call make):
一个简单的 Hack,如果需要一些时间才能打印大部分输出(就像你调用 make 时),它可以工作:
Scroll up a very little bit (e.g., one line) immediately after executing the command. Since you're no longer at the bottom, this prevents most terminals from auto-scroll down. As soon as you think that output has been printed (scrollbar position and feeling are good indicators), you can scroll down. Just avoid to scroll to the very last line while it's printing.
执行命令后立即向上滚动一点(例如,一行)。由于您不再处于底部,这会阻止大多数终端自动向下滚动。一旦您认为输出已打印(滚动条位置和感觉是很好的指标),您就可以向下滚动。只是避免在打印时滚动到最后一行。
回答by fre837
I pipe the output to less, for example command | less, because it has tons of useful keys for quick navigation plus it supports search. You also automatically start at the very start of output.
less例如command | less,我将输出通过管道传输到,因为它具有大量用于快速导航的有用键,而且它支持搜索。您还可以在输出的最开始自动启动。
Here are some useful commands for quick navigation:
以下是一些用于快速导航的有用命令:
forSPACEto move forward a page,bto move back a page. A page refers to terminal window size worth of output.gto go to the very start,Gto go to end,[n]gto jump tonth line. For example5gwill jump to 5th line./patternto search for a term then navigate withnfor next andNfor previous occurrence. Supports RegExp.qto quit.
f或SPACE向前移动一页,b向后移动一页。页面是指输出的终端窗口大小。g到最开始,G到最后,[n]g跳到n第 1 行。例如5g会跳到第 5 行。/pattern搜索一个词,然后导航n下一个和N上一个出现。支持正则表达式。q退出。
For more info just do man less. Man pages also use lessby default so all of the above works.
有关更多信息,请执行man less。手册页也less默认使用,因此上述所有内容都有效。
回答by Aurèle
We could clear the terminal prior to running the command, with Ctrl-L.
我们可以在运行命令之前使用Ctrl-清除终端L。
Then after running the command, come back all the way up with Ctrl-Home.
然后在运行命令后,一路返回Ctrl- Home。
回答by zaSmilingIdiot
Is it not easier to simply redirect the output to a file, thereby keeping your terminal window "clean"?
简单地将输出重定向到文件,从而保持终端窗口“干净”不是更容易吗?
something like: command >> output_file
就像是: command >> output_file
回答by Varun
If you work with screen, I think there is a way.
如果你和 一起工作screen,我认为有办法。
- Start a session with
screencommand - Execute your command (which outputs some data on your screen)
- Type
Ctrl+afollowed by[. screen enters copy mode. - You can search your command prompt backwards with ? (just like vi)
- 使用
screen命令启动会话 - 执行您的命令(在屏幕上输出一些数据)
- 键入
Ctrl+a后跟[. 屏幕进入复印模式。 - 您可以使用 ? (就像vi一样)
Also screenhas lot of advantages. Check http://kb.iu.edu/data/acuy.html
也screen有很多优点。检查http://kb.iu.edu/data/acuy.html

