GNU Screen, Part 2

Please read GNU Screen Basics first.

If you have a lot of screen real-estate for running screen, you may want to use "splits". This allows you to divide the visible terminal into two or more sections, each of which contains a window. To start trying this, run screen and then press 'Ctrl-a S (capital "S", different from small "s"). This will create a horizontal split. Or you can create a vertical split with 'Ctrl-a |' (pipe). And if you want to get fancy, you can create one within the other, dividing the visible screen up multiple times. The default behaviour of screen is to create each of these splits empty, without a shell inside them. To navigate to a split, press 'Ctrl-a <tab>' once (or several times) until the split you want is active. You can then create a new shell with the familiar 'Ctrl-a c' command, or select an existing window to show in the split by the other navigation commands you should know such as 'Ctrl-a <N>', or 'Ctrl-a "' (double-quote).

It should be noted that exiting a window or shell in screen doesn't affect the split it's in at all. And vice versa: you can kill a split with the 'Ctrl-a X' (capital "X") command, but this doesn't kill the window/shell - it's still running and available.

I've now introduced you, indirectly, to two incredibly nasty commands. 'Ctrl-a x' (small "x") is the "lock" command, and 'Ctrl-a s' is the "xoff" command. The "lock" command is detrimental to your splits, as it kills all of them (and you have to unlock the session). What use exactly is a terminal lock when everyone is using a GUI? And "xoff" essentially makes your window unusable until you send "xon" ('Ctrl-a q'). Both these commands used to serve a purpose but are now useless. I would strongly encourage you to make your own life a lot easier:

# add this to your ~/.screenrc

# "X" is "remove", which kills a split, but small "x" is lock which is
# not only useless, but kills all splits once you log back in:
bind x remove

# similarly, "s" is the useless and deeply problematic "xoff" command (which
# effectively locks your window), but "S" is the useful "split" so make
# both "s" and "S" "split"
bind s split

I encourage you to keep the comments so you know what and why you're doing this.

"Kill" is also a good command to know. To kill the current window/shell, use 'Ctrl-a k'.

Scrollback is different from normal terminals in screen - it's accessed with 'Ctrl-a [' (open square bracket). It may not be immediately obvious that it's worked: you should see a different status bar at the bottom (telling you you're in copy mode, and your relative position in the scrollback buffer), and you can now use the up and down arrows to navigate the buffer. Use '<Esc>' to exit this mode.

Last and possibly most important, detaching and reattaching screen sessions. In the previous blog entry about screen I advertised screen as a way to avoid your current ssh session being ruined when it's dropped. You can deliberately detach a screen session with 'Ctrl-a d'. As mentioned last time, none of the programs running inside screen even know that anything has happened: they just keep running. To check for running screen sessions, use screen -ls which will give you a list of screen sessions for the current user on the current machine, and the session number of each. If there's only one session, you can reattach with screen -x, but if there are several you need to specify a session number: screen -r <session-number>. Keep in mind that the detach-reattach cycle (like "lock" above) kills all splits.

screen has many, many more tricks up its sleeve: but for me, these are enough commands to make it very useful.