32
votes

I've got a program which has a lot of 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:

Is there a way to easily have the terminal scroll back to the part where the last command was given?

10
For anyone finding this who is using Windows and ConEmu, you can use Ctrl+Alt+RePagFran Cano

10 Answers

28
votes

I have just found this in Terminal for OSX:

Edit > Navigate > Jump to Previous Mark: cmd + UP.

4
votes

I use iTerm2 in macOS, I came up with a method. First, you should make sure you check the Unlimited scrollback in iTerm2's preferences. enter image description here

After you run a command in terminal and got a long long output.

Press Cmd+F(maybe ctrl+F in windows) then your can search in terminal like this:

enter image description here

Finally, just search your user name and press Enter, generally speaking you will jump to the last command:

enter image description here

3
votes

If you're using a terminal like GNOME Terminal, you can search backwards. For example, Ctrl+Shift+f then enter either the literal command or a regular expression to match it (and make sure "Match as regular expression" is set accordingly).

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.

3
votes

ACHIEVING IT IN LINUX

Add to your prompt an identifier (for example add in the bottom of your .bashrc):

promptCount=0
PROMPT_COMMAND=promptCount=$((promptCount+1))
PS1="/\$promptCount\\ $PS1"

Now use the search funcionality of the terminal, perhaps Ctrl + Shift + F, for the prompt line you wish.

Making it more handy:

Install xdotool (command-line X11 automation tool)

Create a function that will press the exact keys that you would press when finding the N prompt line through the searching functionality (add in the bottom of your .bashrc):

gp () { xdotool key ctrl+shift+F; xdotool type /$1\\ ; xdotool key KP_Enter;sleep 0,2; xdotool key Alt+F4; }

(At least these are the exact keys that I would press to do it in Manjaro Xfce)

Now:

enter image description here

There is a little frame not shown in the gif where the little search window appears, the identifier for N is written, key Enter is pressed and then the windows is closed. You can try to make this faster changing the value of the argument given to the sleep command. In my case it works well until 0,11 or so.

2
votes

A simple Hack which works if it takes some time until most of the output is printed (like when you call 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.

2
votes

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.

Here are some useful commands for quick navigation:

  • f or SPACE to move forward a page, b to move back a page. A page refers to terminal window size worth of output.
  • g to go to the very start, G to go to end, [n]g to jump to nth line. For example 5g will jump to 5th line.
  • /pattern to search for a term then navigate with n for next and N for previous occurrence. Supports RegExp.
  • q to quit.

For more info just do man less. Man pages also use less by default so all of the above works.

0
votes

Is it not easier to simply redirect the output to a file, thereby keeping your terminal window "clean"?

something like: command >> output_file

0
votes

If you work with screen, I think there is a way.

  1. Start a session with screen command
  2. Execute your command (which outputs some data on your screen)
  3. Type Ctrl+a followed by [. screen enters copy mode.
  4. You can search your command prompt backwards with ? (just like vi)

Also screen has lot of advantages. Check http://kb.iu.edu/data/acuy.html

0
votes

We could clear the terminal prior to running the command, with Ctrl-L.

Then after running the command, come back all the way up with Ctrl-Home.

0
votes

On MacOS with iTerm2:

You have to have Shell Integration installed (iTerm -> Install Shell Integration) and open a new tab afterwards.

Press Cmd+Shift+Up to jump to the last mark. Also available under Edit -> Marks and Annotations -> Next Mark.