GNU Screen: Working with the Scrollback Buffer

GNU Screen is a UNIX tool that allows multiple console applications to be run, each in its own “window”, from the same terminal. In a single Screen session, you can run interactive shells, mail programs, SSH sessions, and other console based applications, and you can easily switch between these using hotkeys. You can even split up the Screen display so that multiple Screen windows can be viewed at the same time.

If you’ve never used Screen, but frequently use console applications, it is definitely a tool worth exploring. An introduction to Screen can be found on the Kuro5hin website.

In this article I share my experience with one of my favorite screen features: its scrollback buffer. As you interact with a Screen window, Screen stores a configurable number of lines of history in its scrollback buffer. The scrollback buffer makes it easy to browse or even search through the history of your windows. In addition, it makes it easy to copy and paste any section of text from the history.

Configuring the Scrollback Buffer

By default, the scrollback buffer only keeps the last 100 lines of text, which is not enough for my typical interaction with Screen. I’ve found a setting of 5000 lines to be more than adequate for my usage. The number of scrollback lines can be configured in your $HOME/.screenrc file, by adding the following line:

defscrollback 5000

This sets the scrollback to 5000 lines.

You can also override this default value when starting screen using the -h [num] option, where num is the number of scrollback lines.

Finally, if you want to change the number of lines of scrollback for a single window, using the “scrollback” command. Hit C-a (Ctrl-A) : to go to the Screen command line and type scrollback num, where num is the number of scrollback lines.

You can check the number of scrollback lines in your window. Hit C-a i to display window information. You will see a status line with information similar to the following:

(27,42)/(186,42)+20 +flow UTF-8 3(bash)

In this case, my scrollback is 20 lines (it is displayed as +20 in the output above).

Entering Scrollback Mode and Navigating

To enter scrollback hit C-a [. A status line will indicate that you've entered copy mode. To exit scrollback mode, hit the escape button.

Navigating in scrollback mode will be pretty familiar to VI users. Here are some of the most common navigation keys (taken from the screen manpage):

h -    Move the cursor left by one character
j -    Move the cursor down by one line
k -    Move the cursor up by one line
l -    Move the cursor right by one character
0 -    Move to the beginning of the current line
$ -    Move to the end of the current line.
G -    Moves to the specified line 
       (defaults to the end of the buffer).
C-u -  Scrolls a half page up.
C-b -  Scrolls a full page up.
C-d -  Scrolls a half page down.
C-f -  Scrolls the full page down.

I often use the page up and page down commands to quickly scroll back through the window's history.

In addition to traditional navigation, Screen allows you to search the scrollback buffer using the following commands:

/ -    Search forward
? -    Search backward

Search is a very useful feature. For example, you could run a script and search for keywords in the output (such as Error), without having to redirect the output.

Copy and Paste

Scrollback mode is also know as copy mode and it allows you to copy any section of text into a copy buffer. To copy text, move the cursor to the start of the text you want to copy, hit spacebar, move the cursor to the end of the text you want to copy (Screen will highlight the text to be copied as you move), and again hit spacebar. Screen will indicate the number of characters copied into the copy buffer.

To paste text, simply hit C-a ].

Copying to the Mac Clipboard

While copying and pasting in a terminal is very useful, I also find that I often want to copy some text from a terminal into my clipboard. This next tip will show you how to do this for Mac OSX, but I'm sure it can be easily modified to work with other operating systems.

Open $HOME/.screenrc and add the following line:

bind b eval "writebuf" "exec sh -c 'pbcopy < /tmp/screen-exchange'"

This line tells Screen to write its copy buffer to a temporary file (defaults to /tmp/screen-exchange), and then sends that file to pbcopy, a Mac OSX utility that copies text into the Mac clipboard. In this case, I’ve bound the command to C-a b, but you can change to best suit your own environment.

Conclusion

This wraps up my review of Screen’s scrollback buffer. I hope this tutorial is useful, especially to those that frequently work in terminal windows.

Do you have interesting ways of using Screen’s scrollback buffer, or Screen in general?

Comments

Add a Comment
  1. I was here for the PHP widget plugin, but then saw this article. Great information :) I was using screen just to avoid disconnection and resume working sessions. Don’t even know it can scroll back, well, never bothered to look for it. Hey but thanks, now I know it’s there and how to do it :D

  2. [...] a nice description of how to use screen scrollback feature. Turns out you can search in scrollback buffer and use vi-like move commands. [...]

  3. [...] Screen: an introduction and beginner’s tutorial GNU Screen: Working with the Scrollback Buffer TIP Using screen (gentoo-wiki.com) This entry was written by stefan and posted on 21. Mai 2007 at [...]

  4. Peter-

    Yes, screen is awesome. Wait until you figure out how to split the screen and turn on monitoring. Screen is one of those apps that makes me wonder how I ever lived without it on the Linux command line.

  5. Love it! Great article. I use screen religiously, and am pleased to have found this tip. One of my favorite features is the logging feature (Ctrl-A H)

    I’d love to see another article that covers how to use split screen/monitoring, and maybe another that covers multi-session (screen -X), which can be used to ’show’ someone how to do something. Once in a while we need to allow a vendor access to something, but I like to use screen so we can both see what they are doing (and log it!)

    I can’t figure how to use -X from two different users though.

  6. I use the following for multiuser sessions.

    ~/.screenrc:
    # allow people to attach to my screen
    multiuser on

    # allow the following people full control
    addacl root,aledesma,mymanager,mycoworker1,mycoworker2
    aclchg aledesma +rwx “#?”

    # allow everyone readonly access
    aclchg *,!aledesma -rwx “#?”

    # setup 10,000 lines of available scrollback — copy/paste
    defscrollback 10000

    # fix scrollback in putty
    termcapinfo xterm* ti@:te@

    _EOF_

    Besides that you may have to have all users in a special group and setup PTYMODE and PTYGROUP(config.h) for your environment OR run screen setuid root. Depending on how SOCKDIR(config.h) is setup on your system you may have /tmp/uscreens, /tmp/screens, /var/run/uscreens, /var/run/screens, or the fail back into the users ~. With that you will have to insure that the users S-$USER directory is available to the other users OR again that screen is running suid root.

    Good luck :)

  7. Lakshmi NarasimhanNo Gravatar says:

    November 5th, 2007 at 1:12 am

    Hi,

    Thanks. I was searching around the net to increase the scrollback buffer length on a screen’d session but without success till I found this article. Thanks..

  8. Thanks for the page! This is exactly what I needed!

  9. Thanks @aledesma for your hint:

    “…
    # fix scrollback in putty (edit: and for Mac Terminal)
    termcapinfo xterm* ti@:te@
    …”

    I’ve searched very long for a solution of my problem. Don’t know if it works in putty, because it works very fine, but I got a new MacBook and the “Terminal” won’t show me the scrollback. The scrollbar is not there and the only method to scroll back is with the key command C-a [ but this is not nice for everyday usage.

    As I said this works for me with the MacOSX Leopard “Terminal” but not with iTerm.

    Thanks a lot!!!

  10. I was “inspired” by the integration of the clipboard using pbcopy that I wrote something similar to work with Windows and remote sessions as well. See my page for details.

  11. Perry: very cool idea! Thanks for posting the link.

  12. As a fanatic screen user, I just was messing in mysql console. After hours of work, I’ve managed to enter 160 emails in an SQL-table.
    Unfortunately I typed “delete from mailinglist where 23″ instead of ‘id=23′.
    Result : all work gone :/

    luckily I was inside a screen, and my previous command was ‘SELECT * from mailinglist’.
    With this tutorial, I was able to get back all data back into the database :))

  13. Can anyone get the split screen to work when using a macbook w/ leopard. I’m not sure if some app is stealing my keyboard shortcut of Ctr-a–S or what. I am ssh’d in to a server of mine and actually running screen on that box. However, I can’t get the screen to split. I have no problem when logged in directly on the console. This is happening on Fedora,Debian, RHAS and Centos boxes

  14. You can copy to the clipboard on Linux by replacing “pbcopy” with “xsel -i -b”.

  15. Wow this helped me a lot! I have red several manuals for GNU Screen and used it for a long time (~1y) and I have complete missed this wonderful feature.

  16. [...] If you enjoy the command prompt in Linux/OS X as much as I do, then I hope you’ve been aquainted with screen.  If not, think of it like tabs in your browser.  Except now you can go to a different computer and pull up your browser with all those same tabs already opened.  The one thing that sucks about screen is that you can’t scroll up to see the history.  This makes it difficult if a bunch of shit is being dumped to the terminal and you need to look through it.  Well, it turns out that it does exist, and it’s way more awesome then I ever could have imagined.  It’s got a search feature!  Interested?  Read up on it here. [...]

  17. note that that’s ctrl+a and then a colon, then type ’scrollback num’ for the scrollback buffer.

  18. [...] a great post on using GNU screen on MacOSX — and yes, the copy to pasteboard under tiger [...]

  19. [...] a while back I wrote an article about how I use GNU screen’s scrollback buffer. In the article I detailed how to copy the scrollback buffer into the Mac clipboard, which made it [...]

  20. Gavin SinclairNo Gravatar says:

    January 9th, 2009 at 5:43 am

    Very cool; thanks.

  21. Dang. Thanks.

    Did’nt realise the C-a [ option was required to view scrollback.
    The individual scrollback setings per window helped too ;)

  22. Has anybody notice that scrolling with mouse does not work in when using screen via putty? It mixes the scroll back content from multiple windows.

    Mouse scroll back works fine in putty when not using screen.

  23. Anyone got any fixes for the issue using Putty and screen ?

    If I try and use the scroolbar to scroll back, and I have a hardstatus line, I get the images of other screen session and the
    hardstatus line being scrolled up?

    thanks
    Neil

  24. Cool, thanks for this. I’m using Pimp to send blocks of code to an ipython session, and now I can copy from the history of my session back to MacVim w/o the mouse!

  25. Thank you for a very comprehensive article. I’ve been using screen for years and the scrollback always bugged me. I’m surprised to only find out about the scrollback buffer now :s

  26. [...] Also this guy has a nice screen (Scroll back and copy / paste) tutorial @ LINK [...]

  27. I just found this tutorial, thank you google.
    I was wondering, is there a way to span multiple terminals thanks to screen?
    What I would like to do is say display 130 lines of history in one terminal, and the history from 131 to 260 in another, so that I have 260 lines scrolling up in two columns when I type something… That would be a neat feature to parse big files.

  28. [...] GNU Screen: Working with the Scrollback Buffer by Chris Pettitt [...]

  29. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  30. Thanks for the tutorial! I really wanted to learn scrolling in screen.

  31. hi

    does anyone know if its possible to remap pageup/pagedown to enter scrollback mode automatically? its just too many keypresses right now.

  32. Thanks for the article. It is really useful!

  33. Is there any way that one could still use the scrollbar/mouse interaction on a terminal window to work with screen. I find that I am always trying to scroll with my mouse/scrollbar.

  34. Great article!

  35. [...]   # Scroll-Fix für putty und Mac Terminal termcapinfo xterm* ti@:te@GNU Screen ProjektQuelle: Samsarin Blog (EN)Ähnliche Beiträge:PuTTY (in Bearbeitung) —Sorry, der Beitrag ist in Bearbeitung!— [...]

  36. DUDE!! This website had exactly what I was looking for!! I was wondering how the heck I was supposed to search for something like “screen buffer size” and actually get something related to the “screen” application. “Screen” is such a general term I thought it would be impossible! But fortunately your site was the first link in my first google search! AWESOME!! Can’t thank you enough for this!

    Pz!

  37. ahhhh thanks, had been looking for more information on screen. Excellent. :)

  38. Looking for a way to get screen to scroll through history using the mouse wheel in a gnome terminal on opensolaris. I can get it to work if I use the escape sequence, but would rather not have to do that first. Any ideas?

  39. Years after…

    And yet very usefull !

    Thanks i was looking for a simple way to extend tty Scrollback buffer wich is very limited.

    The only other solution i found was to recompile kernel ^^

  40. Thank you . very good guide. ~

  41. Thank you for the article! Increased my screen’s buffer to 10000 :-)
    I’m also a fanatic screen user, with a hardstatus line!

  42. Thank you. This article helped me become so much more productive.

  43. Thanks.
    This article helped me a lot.
    You published also some great photographies at flickr..
    Greetings and the best wishes to you!

  44. Surprised you didn’t talk about detaching sessions and resuming them on future ssh sessions. this is by far the most useful feature of screen.

    if you use C-a d you will detach from you screen and back to the regular shell session. to resume your session use screen -R.

    This very useful for lots of things when working on remote servers. also very handy for running an IRC client on a remote system so that its always connected. you just ssh into the box when ever you want to use it.

  45. Thank you. This article was helpful!

Comment on this Article