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

I have just found this in Terminal for OSX:

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


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


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.


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.


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.