GNU Screen: Working with the Scrollback Buffer

by Chris Pettitt

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?