How to scroll back the terminal to where the last command was given?

11,114

Solution 1

I have just found this in Terminal for OSX:

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

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
11,114
kramer65
Author by

kramer65

Updated on June 03, 2022

Comments

  • kramer65
    kramer65 about 2 years

    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?