47

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?

1
  • 1
    For anyone finding this who is using Windows and ConEmu, you can use Ctrl+Alt+RePag
    – Fran Cano
    Commented May 9, 2019 at 10:20

11 Answers 11

36

I have just found this in Terminal for OSX:

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

2
  • 8
    This option is amazing, wish I could find an equivalent in Linux terminals.
    – gib
    Commented Aug 12, 2017 at 12:46
  • 3
    If you resize your terminal window between running the previous command and Jumping to the previous mark, it will not be accurate. I assume the previous mark is line based, so the jump is going back to that line, which is now different content based on the new window width.
    – blobdon
    Commented Jan 27, 2019 at 15:49
9

On MacOS with iTerm2:

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

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

3
  • 2
    I think this only works if you set a mark first, right? Shortcut with ⇧+⌘+M.
    – phip
    Commented Feb 13, 2022 at 5:16
  • 1
    This works with Shell Integration installed, as I mentioned. Then iTerm puts a mark next to every command you entered automatically.
    – caarrrl
    Commented Feb 14, 2022 at 7:12
  • Yes, it depends on Shell Integration. In a much similar way to how OSC 133 prompt + scroll integration works for other terminals such as kitty, foot, and hopefully eventually tmux + tmux/tmux PR #3596
    – TrinitronX
    Commented Jul 31, 2023 at 18:15
6

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

1
  • 2
    Its a hack but I do like your idea
    – Jay Modi
    Commented May 9, 2019 at 3:33
5

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.

3

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.

1
  • This is the only answer that is useful for Linux. The less command is a mess in my terminal, so I will be using Ctrl Shift f and search for my user name for now.
    – bruno
    Commented Mar 14, 2020 at 13:19
3

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.

3

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.

1

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.

1

VTE based terminals, such as e.g. GNOME Terminal, GNOME Console, Xfce4 Terminal, Terminator, Tilix, Guake and more, support this feature since the GNOME 46 (VTE 0.76) release in March 2024.

The hotkey is hardcoded for the time being, it's Ctrl+Shift+Left and Ctrl+Shift+Right.

Under the hood, VTE uses the quasi-standard OSC 133 shell integration escape sequences to see where the prompts are, like several other terminals already do.

0

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

something like: command >> output_file

2
  • Yes I've done this before. But 90% of the time, the run is useless in the sense that I don't need the results further. After a while, that folder simply started clogging up with useless files. I can of course overwrite the same couple files, but I still have to open the files again, even though I often just take a couple seconds to look and then move on again.
    – kramer65
    Commented Jun 20, 2013 at 12:02
  • Have you tried then something like: command > /dev/null 2>&1. That should redirect output to the nothingness of /dev/null, and will also then redirect errors to the standard output stream Commented Jun 20, 2013 at 12:49
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.