586

I use screen for my command-line tasks while managing the servers where I work. I usually run small commands (mostly file-system tasks) but sometimes I run more extensive tasks (like DBA).

The output of those tasks is important to me. Since I use Ubuntu and OS X (both Terminal Windows) for my tasks, yet I need to use screen, the scrolling is not available, so any long output (think a 500-row table from a select) is invisible for me. Mouse-wheel is out of the question.

When I say "scroll is invisible for me, I mean this:

top while using screen, in Mac OS X, while scrolling

I was thinking about two options:

  1. Pause (think paginate) the output of a certain command. When the output begins, it would let me read what's happening, then I press "Enter", then the output continues until there's nothing more to show.

  2. Scroll inside screen. But I don't know if this is possible.

Of course, I don't know if those options are actually possible. If they are, how can achieve them? Other alternatives will be well received.

corning
  • 107
AeroCross
  • 6,403
  • 2
    FYI BSD solved this decades ago. Scroll lock will pause any scrolling buffer in any terminal or screen. Where is this in linux!!!!! – Justin Goldberg May 17 '21 at 13:49

7 Answers7

798

Screen has its own scroll buffer, as it is a terminal multiplexer and has to deal with several buffers.

Maybe there's a better way, but I'm used to scrolling using the "copy mode" (which you can use to copy text using screen itself, although that requires the paste command too):

  • Hit your screen prefix combination (C-a / control+A by default), then hit Escape or [.

  • Move up and down with the arrow keys ( and ).

  • When you're done, hit any key except arrow keys, numbers, and certain letters to get back to the end of the scroll buffer. Most people use q or Escape

(If instead of exiting you press Enter or Space and then move the cursor, you will start selecting text to copy, and pressing Enter or Space a second time will copy it. Then you can paste with C-a followed by ].)

Of course, you can always use more and less, two commonly used pagers, which may be enough for some commands.

njsg
  • 13,495
  • 5
    Also, pressing q quits copy mode without copying anything (and it's one less button press). This might be in the vi explanation but the link is now broken. – Nathanael Farley Jun 26 '14 at 08:59
  • i found another source for info: http://commandliners.com/2009/12/screen-working-with-the-scrollback-buffer/ – nocache Apr 30 '15 at 08:22
  • 3
    Also pressing [Esc] exits from copy mode too. – Sopalajo de Arrierez Oct 06 '15 at 21:06
  • 10
    any way to page-up? – Sungguk Lim Oct 29 '16 at 02:47
  • 13
    Scroll buffer seems to support vi-like commands: C-b and C-f for scrolling page up/down. – Touko Jan 18 '17 at 07:30
  • 6
    In copy mode my PageUp and PageDown buttons work. First press only moves one line but after that it pages like normal. – reas0n Feb 28 '17 at 21:54
  • There is no such thing as the "Return" key on Windows. – dtbarne Nov 03 '17 at 00:03
  • 1
    q also exits copy mode. – Walf Dec 06 '17 at 02:14
  • 1
    I finally had some time to sit down and rewrite this answer taking into account some of the comments. @NathanaelFarley three years is probably a long time, but q is now there (Cc: @Walf). @Sopalajo as is Esc two years later. – njsg Dec 08 '17 at 05:01
  • @dtbarne Key naming is possibly a bit more complex than that — Enter and Return should be two different keys. As this also works with the key known as "Enter", I put both names there. – njsg Dec 08 '17 at 05:10
  • 2
    Did the default prefix combo change? On Ubuntu 16.04, C-A/C-a didn't do anything for me... – polynomial_donut May 03 '19 at 06:27
  • This is a global KDE shortcut. It can be disabled in System Settings | Shortcuts | Global Shortcuts | KDE Deamon – kap Jun 11 '19 at 09:54
  • Note to myself: ctrl+a - esc to enter copy mode, ctrl+b/ctrl+f to move up/down 1 page, ctrl+u/ctrl+d to move up/down half page. Ref: https://www.cyberciti.biz/faq/scroll-up-down-look-at-data-in-gnuscreen-using-pageup-pagedown-keys/ – THN Nov 18 '19 at 05:24
  • 1
    @polynomial_donut I thought it was not working for me as well on my Debian 10 server but it was because I read too fast and was confusing the word "escape" with "space" (which in my native language French is written "espace", very close to "escape" in English, just 2 letters are swapped) :D – baptx Apr 24 '20 at 16:45
  • Does not seem to work with VirtualBox running Ubuntu Server on macOS – Jayden Lawson Aug 27 '22 at 00:37
282

Using the screen buffer as pointed out by njsg is a good solution. You can also disable the alternate text buffer in the xterm termcap info inside screen. When disabled you can use the scroll bars (and mouse wheel) to scroll up and down.

Add this to your ~/.screenrc.

# Enable mouse scrolling and scroll bar history scrolling
termcapinfo xterm* ti@:te@

You can read more discussion here.

George M
  • 13,959
  • 3
    set scrollback limit by typing Ctrl+A, :, type "scrollback 10000" or whatever numeric value you want – lobi Nov 10 '16 at 22:20
  • 7
    Using this, the typical shortkeys of Linux terminals (shift+pg up/down home/end) also work, thanks. – tokland Nov 22 '16 at 10:02
  • 21
    This tip alone makes me reconsider using screen... – Fish Monitor Aug 18 '17 at 10:56
  • 19
    only gives you the output screen gave you since you started it - ie, reattaching elsewhere means you can't scroll up and see what happened prior. but you can still retrieve earlier output using the solution in the accepted answer - then you can use your mouse wheel again. – Phil Nov 02 '17 at 04:08
  • Where can I find this hidden file .screenrc on the remote server? I log in to my school server using ssh and when I start a screen I cannot scroll back unless I use C-a – Ruzayqat Feb 12 '18 at 04:15
  • 1
    @Ruzayqat .screenrc sits at your home directory. It probably doesn't exist yet, in which case you can create it, e.g., echo "termcapinfo xterm* ti@:te@" > ~/.screenrc. – carbocation Jul 31 '18 at 19:18
  • 3
    Disclaimer: The reason we don't do this all the time is that: this will keep screen history within local terminal's scroll buffer. Most terminals will dump their scrollback buffer to disk. If you open many fast scrolling terminals, that may be over hundred megabytes a minute and you will see you are running out of disk very fast on your local machine. – nurettin Jan 21 '19 at 12:48
  • It doesn't work, is there something specific that needs to be done to make it work? – Slake Jan 11 '21 at 12:17
57

All these answers addressed how to navigate within a screen session, but there is a built-in functionality in screen command to store everything in a file through the -L argument according to the manual which reads:

-L tells screen to turn on automatic output logging for the windows.

so you can do:

screen -L -S testscreen

and it will create a file in current directory, with the name screenlog.# where the # is a number for that screen starting from 0.

This has lots of advantages and the most important ones for me are:

  1. Keeping record of what i have done since I can save the log file in the project folder for future reference.
  2. You can inactively and passively monitor the process:
    • use tail -F to monitor the progress in realtime without being attached to the screen.
    • use grep to check for certain term in the log and produce notifications (email, popup, voip, etc.). This can be applied on multiple screens without you actively looking at them.
  • 2
    I was not aware of the -L screen flag, this is very useful! +1 – Kredns Jun 27 '19 at 16:11
  • 3
    just want to add more, screenlog.# is generated in the same folder where you execute screen command – Lê Quang Duy Aug 06 '19 at 03:52
  • Funnily enough, I am using screen because tail is not reliable in the WSL2. Good to know though when outside of WSL. – Jay Killeen Aug 20 '20 at 04:42
  • 1
    @JayKilleen I think you are using a wrong tool ;) try less -F to get the functionality you want: https://linux.die.net/man/1/less (P.S by wrong tool of course I mean Window$) – Mehrad Mahmoudian Aug 28 '20 at 08:20
  • Is there a way to combine this with "termcapinfo xterm* ti@:te@" suggested in the other answer to have the best of both worlds? Or will that just result in an empty screenlog.#? – Kvothe Feb 27 '24 at 21:01
  • @Kvothe As far as i understand, these two are not mutually exclusive. So you should in principal use them together. I personally have moved all my scripts to tmux and I only use screen for remote machines that I cannot install tmux on. So for me the -L is sufficient improvement. – Mehrad Mahmoudian Feb 28 '24 at 16:21
8

If you didn't get it clear checkout

https://stackoverflow.com/questions/18489216/how-to-scroll-up-and-down-in-sliced-screen-terminal

Try Control+A, then Escape.

Then navigate with Up,Down or PgUp PgDown

And Escape to quit that mode.

Otherwise https://unix.stackexchange.com/a/40243/318478

answer on top ! Just explain all.
The command may be unclear! Well i missed it at first. Look at the portion about how to copy! It's cool!

  • You answer works much better due to being a brief answer. Original answer has too much words so at glance I haven't noticed that I actually need to press Escape after Ctrl+A. Lol – Jerry Green Aug 17 '23 at 19:49
5

I've had success getting basic paging by piping content to more, for example:

ls -l | more

Or, if you want color output for use cases like syntax highlighting, you can use

ls -l --color=always | more

This results in output that I can easily page through one screen at a time. I haven't tried @uther's modification to ~/.screenrc but that does seem preferable as a lasting solution when a mouse is available.

To try this out, you can go to this BusyBox emulator and then...

cd ~/bin
ls -l --color=always | more

Use the spacebar to page through the results of the ls -l command or use the enter key to move through results line by line.

1

If you are on Windows and using Bitvise SSH Client then you can easily scroll the output when using screen. I use it all the time without any trouble. Even if you detach the screen and attach again, you can still scroll the previous output in that screen.

When using Putty or Mac Terminal, the suggestion by @njsg works great.

AbsarAkram
  • 111
  • 2
1

For Emacsians you can add this line in your ~/.screenrc file to gain Emacs-style navigation:

markkeys "l=^F:h=^B:$=^E:^F=^V:^B=^U"

Here're what changed by this line:

| Action      | Default | Emacs |
|-------------+---------+-------|
| Forward     | l       | C-f   |
| Backward    | h       | C-b   |
| End of line | $       | C-e   |
| Page down   | C-f     | C-v   |
| Page up     | C-b     | C-U   |

Note that "Page up" uses C-u to replace M-v because screen doesn't support meta characters.