GNU Screen quick tips

I use GNU Screen [henceforth: “screen”] every single day in a variety of contexts. I’m by no means an expert, but I’ve met a few people who aren’t as familiar with some of screen’s more oblique features, so I’ll give you a walk-through of my setup. If you’re new to screen, this isn’t likely the best place to start. Also, this isn’t a replacement for the man page. I barely know anything, so read the friendly manual and learn even more.

If you already know screen, I’m going to discuss setting up an “outer” screen with an alternate command key as well as using screen’s copy mode to capture text. If you already know how to do that you’ll find nothing new here.

First we’ll start with a very small script I wrote called outer. I run this script on my desktop and it creates a screen whose command key is F12. I do this so that when I connect to remote machines (where the command key is still the default of ^a) I’m able to think less because I don’t have to escape remote command keys.

Here’s a scenario that might shed some light on my explanation. Without overriding the command key, you start screen on your desktop and ssh to a remote host where you start screen. Now if you want to interact with the remote screen session, you have to “escape” your commands to it so that the desktop’s screen doesn’t interpret them. If you type ^a-c your desktop screen is going to create a new window. If you want that new window on the remote host, you have to type ^a-a-c, which can get annoying quickly. With outer, your desktop’s command key is F12, so it doesn’t care at all about your remote ^a’s.

For some gravy, outer also sets up F11 as “windowlist -m” which is a list of your windows in use-order. Since I’ve met a few people who don’t know about windowlist, if you hit command-key-” is a list of your windows in numerical order (windowlist -b). Further gravy, outer also always grabs the current version of your .screenrc before it starts screen.

My shell environment automatically names my screen windows to the hostname when instantiating a shell, so my “windowlist -m” might look like this:

Num Name                         Flags

  0 mutt                             $
  1 puppet                           $
 10 henry                            $
 15 machinex                         $
  8 machiney                         $
  2 jables                           $
 13 machinez                         $
  3 kage                             $
  6 storooni                         $
  4 lee                              $
  7 dude                             $
  9 walter                           $
  5 donnie                           $
 11 maude                            $

You can navigate the windowlist with j/k/arrowkeys and enter.

Screen’s copy mode can be used for capturing its scrollback contents to a file. To enter copy mode, hit command-key-Esc. If you’re familiar with vi/vim, you’re now in something akin to vi/vim’s command mode. Movement keys in this mode are very similar to vi/vim, but not exactly; so read the man page. Once you’re in copy mode, move your cursor to a given point in the scrollback buffer and then hit the space bar. By hitting the spacebar, you’ve dropped a marker at the cursor location. Move again to select text. Once you’ve selected the text you want, hit >. Hitting > will save the selection to a file whose name is the value of “bufferfile”. The default is /tmp/screen-exchange. Yea I know: horrible name. I guess that’s why it’s configurable.

You can also use copy mode to paste into your screen session. The details of that I’ll leave as an exercise to the reader.

I frequently connect to hardware consoles via telnet/ssh, where selecting text from a terminal emulator with a mouse often adds unwanted spaces or new lines. Copy mode is invaluable for preserving the formatting of the consoles’ text.

Leave a Reply