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?
April 15th, 2007 at 9:19 pm
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 
May 2nd, 2007 at 12:48 pm
[...] a nice description of how to use screen scrollback feature. Turns out you can search in scrollback buffer and use vi-like move commands. [...]
May 21st, 2007 at 11:44 am
[...] 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 [...]
July 16th, 2007 at 1:52 pm
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.
August 29th, 2007 at 4:05 am
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.
September 12th, 2007 at 1:18 am
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
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..
February 6th, 2008 at 12:14 pm
Thanks for the page! This is exactly what I needed!
February 11th, 2008 at 1:31 pm
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!!!
February 28th, 2008 at 1:08 pm
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.
February 28th, 2008 at 2:50 pm
Perry: very cool idea! Thanks for posting the link.
March 13th, 2008 at 4:40 pm
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 :))
March 22nd, 2008 at 12:44 am
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
August 14th, 2008 at 11:34 pm
You can copy to the clipboard on Linux by replacing “pbcopy” with “xsel -i -b”.
August 17th, 2008 at 2:48 pm
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.
August 18th, 2008 at 2:36 pm
[...] 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. [...]
August 26th, 2008 at 10:22 am
note that that’s ctrl+a and then a colon, then type ’scrollback num’ for the scrollback buffer.
September 2nd, 2008 at 10:57 am
[...] http://www.samsarin.com/blog/2007/03/11/gnu-screen-working-with-the-scrollback-buffer/ [...]
September 20th, 2008 at 3:05 pm
[...] a great post on using GNU screen on MacOSX — and yes, the copy to pasteboard under tiger [...]
October 18th, 2008 at 9:14 am
[...] 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 [...]