Saturday, September 11, 2010

Top 5 underused GNU screen features

Most people use GNU screen mostly for its abilities to detach from the terminal and create multiple shell sessions. This makes it ideal in combination with remote terminal connections like ssh. There are some features which, although not very popular, are still useful on a number of occasions.

As a bonus, let's start with a short explanation of the popular features. If you're familiar with detaching and multiplexing, you might want to skip to the sections about more rarely used features.


The first thing to know about screen is that all shortcuts start with Ctrl + A by default.

The most common screen workflow is:

  1. Connect to a remote system via ssh

  2. Start/attach screen:

    screen -D -RR

    This command will detach a running screen session and create a new one, if necessary.

  3. Work

  4. Detach screen by pressing Ctrl + A, then D.

  5. Disconnect.

  6. Take a break, return to 1 :)

You can find the currently running screen sessions using the command:

screen -ls

Alternative: nohup is also used when you want to detach a program when you disconnect, but it will never attach to the terminal, so you cannot use it if it's interactive.


When you connect via ssh, one shell is rarely enough. Connecting a second time is often inconvenient and slow. Enter screen- you can create a new session using Ctrl + A, then C. You switch to the next one using Ctrl + A, then space (or n). You switch to the previous one using Ctrl + A, then backspace (or p). Using Ctrl + A and then a digit you can directly jump to the session numbered 0-9.

Alternative: using ssh's option ControlMaster will reuse an existing connection and open another shell immediately, but this is only relevant for ssh.

Now you know the basics, let's see what other goodies screen offers.

1. Sharing sessions

Typing in a terminal is usually a lonely experience. Sometimes you wish that you could show someone else what you're doing or even do it together. It might be because you want to teach someone some UNIX tricks, or you must solve an issue together or you're up to the challenge of remote pair programming. Your wish is granted! In screen, you can make it so that your keyboards and monitors are in control of a single session.

The screen host should do the following:

  1. Ctrl + A then type:

    :multiuser on

  2. Ctrl + A then type:

    :acladd guest

    where guest is the name of the user you want to let in your session

Then the screen guest can join using the following command (which will work if there's a single multi-user session open):

screen -x username/

Alternative: VNC can be used only for graphical remote connection sharing.

2. Copy and paste

It's not always easy to copy and paste text in a terminal. For one, if you're on a getty console (the black screen with the login prompt and no graphics), you can't even use your mouse (usually). If you have a crappy X terminal, you might have the problem that wrapped lines are cut by newlines or it's hard to select the output if it spans more than a single screen... Whatever it is, with screen you can cut like a pro without even using the mouse.

Ctrl + A, then pressing "[" will enter copy mode. When in copy mode, you navigate around using the vi key shortcuts (you know the vi shortcuts, otherwise you wouldn't be reading about working in terminal sessions, right?).

Press space once to mark the beginning and a second time to mark the end of the snippet you want to cut.

Ctrl + A, then "]" will paste the copied text.

Did I mention screen can also copy rectangular blocks of text? Reading about it in the manual is left as an exercise for the curious reader.

Alternative: X-Server's select/middle-click can be used- only if you're running in an X session, though.

3. Log output

We humans are not good at remembering stuff, that's what computers are for. To record exactly what you have typed and what was the output, start logging using Ctrl + A, then H, and the same sequence to stop logging. The logging session is written to screenlog.0 if you're recording in the first window, screenlog.1 in the second one, etc.

Alternative: The UNIX command script will start a new shell and record all keystrokes and output in the file typescript.

4. Monitor for activity

Let's say you've started a long-running command and you're waiting for it to finish while you're busy typing in another window. Switching the window periodically just to check what's going on quickly becomes annoying, so you type Ctrl + A, then M and you're set- screen is watching the console for you. If anything changes in this window, you will see a notification in the status line at the bottom.

Alternative: Many linux terminals can monitor for activity, e.g. konsole- but they're graphical and need to run in an X session.

5. Lock screen

It's often useful to lock the screen session without detaching, especially when using a multi-user session. Ctrl + A, then x, and you're done. Type your password and your session is available again.

Alternative: Popular desktop environments use xscreensaver, but again- this only works in X sessions.

Where to go from here?

If you like GNU screen, you might also try tmux. According to the site, "tmux is intended to be a modern, BSD-licensed alternative to programs such as GNU screen". If you're interested, check out this detailed tmux blog post.

If you want to take screen to the next level, you might give tiling window managers a try. They do for the graphical environment what screen does for the terminal. The idea is that you don't resize your windows, but the window manager does that automatically by partitioning the desktop area in adjacent windows. Most of these also try to use keyboard shortcuts extensively and obviate the need of a mouse. I'm currently impressed by xmonad, although I've heard nice things about the awesome window manager as well (yes, that's its name).