Screen is awesome. No, seriously. GNU Screen, often just screen, is a supershell that lets you run other terminal programs, such as mail readers, text editors, shells, and so on, in a single terminal in the UNIX world. If you get confused with all the terminal emulators you need to run, screen manages them all. Even if you don't, it has enough features for you to use it anyway. And it is awesome.

You may be asking, "Why is screen so awesome? I am a lowly human being, what can a terminal multiplexer (for screen is sometimes called one) do for a poor soul like me?" Well I'll show you.

Silver Screen

I am going to pretend that you have access to screen here. If you do not, you can install it, follow along, or close your eyes and imagine. Whatever works for you.

Starting screen is simple enough:

$ screen

If you try that, screen will launch, and present you with... another shell, not entirely unlike the one you were at previously. But do not be fooled! Screen created a window with number 0, and started a shell on it. Try doing a ls, which is the first thing anyone does, and you'll find out that it works just like any shell.

Now, create a new shell by typing C-a c, or Control-a c. Your ls will have gone, and is replaced by a new, unused shell.

Where did the old shell go? Try pressing C-a C-a, and you'll be back at shell #0 (ls). C-a C-a is the switch window command, and it switches between two windows - in this case, window 0 and window 1.

Try making a third window - C-a C-a again, and run a ps aux to distinguish it from the others. This is window #2.

To get back to the first window (0, the one with ls), use C-a 0. C-a C-a won't cut it here, as it changes between the two most recent windows, neither of which is window 0. Similarly, C-a 1 goes to window 1, C-a 2 switches to window 2, and hopefully you should see a pattern emerging here.

Alternatively, C-a n goes to the next window in the list, and C-a p goes to the previous one.

Summary of new commands:
screen    = Load screen
C-a C-a   = Switch between two windows
C-a c     = Create a new window
C-a n     = Go to the next window
C-a p     = Go to the previous window
C-a <X>   = Go to window (Where X is a number)

Movie Screen

All these windows can get pretty confusing. If you've been following this node, you should have three windows open - 0: ls, 1: empty, 2: ps aux. Now, suppose you leave your computer to go to a dance lesson, and when you come back you've forgotten what all the windows are for.

But not to worry! Screen can show you the window list, when you press C-a ". Do that. You'll get a list, like this:

Num Name   Flags
  0 bash   $
  1 bash   $
  2 bash   $

Screen is smart enough to tell you that you are running three shells, but not clever enough to tell you what they were for - it just knows the name of the shell running in them.

This is where screen renaming comes in. Say you want window 1 to be your editor window. Switch to window 1 (C-a 1, remember?), and load vi, or emacs, or ed, or whatever. Now that you know that you're in an editor window, you can rename it by using C-a A - backspace over the "bash", and call it whatever you want. Our new window list:

Num Name   Flags
  0 bash   $
  1 editor $
  2 bash   $

We've got an editor, and two shells. Do we really need that extra shell? No, go to window 2 (C-a 2) and kill it by pressing C-a k. Answer y to the prompt to kill it real good.

Num Name   Flags
  0 bash   $
  1 editor $

It's always nice to have a tidy window list, like we do here.

Summary of new commands:
C-a "     = Show a list of windows
C-a A     = Rename the current window
C-a k     = Kill a window

Super Screen

Undoubtedly the best feature of screen is the ability to detach and reattach to a screen. Go back to screen, it doesn't matter what you're doing, and type the detach command, C-a d.

detached $

All your windows disappear, and you're unceremoniously placed back at the old shell.

What just happened? Screen did not close itself, it just hid itself - all your applications are still running!

At the prompt, you can then reattach the screen:

$ screen -r

And your windows will be there, unchanged. If you were in insert mode in Vim before, you'll be in insert mode now, too.

This is incredibly useful if you change computers a lot - using screen and ssh, you can take your environment with you wherever you go:

  1. ssh-agent, screen, set up your mail reader and editor
  2. Detach screen (C-a d), leave for school/workplace
  3. Arrive at workplace, telnet, reattach screen (screen -r)
  4. Get on with whatever you were doing

This is where the true awesomeness of screen lies. Some people have had single sessions of screen running for over a month, leaving their computer and reattaching screens whenever they need to.

Summary of new commands:
C-a d     = Detach from this session
screen -r = Re-attach to a session

On Screen

Hopefully you should be feeling the awesome power of screen by now, somewhere in the back of your cerebellum.

Ever wanted to watch your bittorrent downloads while on irc at the same time? Here are some other nice things to do in screen:

  • Watching a screen for noise (C-a M), telling you when someone talks in irssi or when you get mail in mutt
  • Watching a screen for silence (C-a _), telling you when your compilation is finished
  • Viewing the scrollback of a program (C-a escape), to see what you've been missing
  • Copying (C-a [) and pasting (C-a ]) lines from a window
  • Splitting a window into two new ones:
    • Use C-a S to split a window in half
    • Use C-a Tab to switch between them
    • Use C-a Q to join them together again
  • Nice help system (C-a ?) for when you get lost

Smoke Screen

Every respectable program has a .programrc, and screen is no exception. Here are some lines to put in ~/.screenrc:

escape ^zz
Remaps C-a to C-z. Useful if you need to use C-a in emacs.

startup_message off
Turn off that annoying message at startup

nethack on
Error messages from the Dungeons of Doom

hardstatus on
hardstatus alwayslastline Turning on the bottom status line, showing the windows. Very useful

hardstatus string "%{rk}%H %{gk}%c %{yk}%M%d %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"
Complex status line that shows the time, amongst other things

hardstatus string "%{= kc} %?%-Lw%?%{= kY}%n*%f %t%?(%u)%?%{= kc}%?%+Lw%?"
A much more simple status line

And that's pretty much everything covered. Have fun using screen!

This writeup was screened using screen on a screen. Yeah.