What screen does
Screen is best described as a terminal multiplexer. Using it, you can run any number of console-based applications--interactive command shells, curses-based applications, text editors, etc.--within a single terminal. The desire to do this is what gets most people hooked on screen. I used to start a half-dozen terminal emulators when I logged into my machine: I wanted one to read my email, one to edit my code, one to compile my code, one for my newsreader, one for a shell into my web host, and so on. Now I start one terminal emulator, and run screen in it. Problem solved.
The other main cool feature of screen is its ability to decouple the terminal emulator from the running programs. This means that you can use screen to keep programs running after you accidentally close the terminal emulator, or even after you log out, and later resume right where you were. It means that the idea of a "session" in which you are running a number of console programs is a free-floating entity that you can bind to any terminal anywhere, or no terminal at all if you want.
Getting started with screen: launching and switching between programs
Start screen just by typing screen at your favorite command shell prompt. You'll probably be greeted by a welcome message. Dismiss this and you'll have with an empty terminal containing a shell prompt, which is pretty much what you had before you started screen. What happened?
Every program running under screen runs in a window, and every window is identified by a unique number. Screen made a new window, numbered it 0, and started a command shell inside it. Type something in your new window so you'll be able to recognize it when you switch to it later.
Now make another window; this will be window 1. To do this, type C-a c; that is, type Ctrl-a and then type c (mnemonic: create window).
Now that you have two windows, try switching between them. To do this, type C-a C-a, which will switch you to whichever window you were using before the current one. Some other useful window switching methods, which you'll need if you plan to run more than two programs:
- Use C-a n and C-a p to switch to the next or previous window in the list, by number.
- Use C-a N, where N is a number from 0 to 9, to switch to the corresponding window.
- Use C-a " to get a full-screen list of windows. You can navigate this list with the arrow keys (or vi-style, with j and k), and pick a window to activate by pressing Enter when it's highlighted. C-a w will give you a small, non-interactive list of windows.
When you're using a window, type C-a A to give it a name. This name will be used in the window listing, and will help you remember what you're doing in each window when you start using a lot of windows.
Exiting the last program in a window will cause the window to disappear. You can also kill misbehaving programs with C-a K.
Detaching and reattaching: the magic of terminal decoupling
If you did the exercise above, you have successfully created a screen session. You can detach from this session by pressing C-a d. You can also detach just by closing the terminal emulator that contains the session. However, keep in mind that neither of these actually end your session. All they do is unbind your session from the current terminal. All of the programs you started running within screen are still running. Really.
Try it: just close whatever terminal emulator you were using to do the exercise above. Then log out, and log back in, if you desire. Start up a new terminal emulator, and type screen -r (the R, obviously, stands for "reattach"). You'll be right back where you were when you detached.
You can probably imagine a lot of good uses for this. You can start all your favorite console programs once and just leave them running in a persistent screen session. Some people have "screen uptimes" of several months.
One other good use for the detach and reattach is as a console-mode "remote desktop" feature. You can detach from a screen session at work, shell into the machine from home, and reattach. Very, very handy. With a bit of extra work, you can even have a number of terminals all attached to the same session--great for collaborative efforts and meetings.
Copying, pasting, and the scrollback buffer
Screen remembers a configurable number of scrollback lines, and you'll need them because you won't be able to use your terminal emulator's scroll features while running screen. You can access the scrollback buffer by entering "copy mode", which is accomplished by typing C-a [. You can mark text anywhere in the scrollback buffer and paste it with C-a ]. Screen is also capable of logging to files, but that's beyond the scope of this tutorial.
Monitoring for silence and activity
One of the disadvantages of running a bunch of programs within screen is that you can't keep an eye on all of them at the same time, since in general you can only see one at once. (Actually, you can split the screen and look at more than one at a time, but that's an advanced topic beyond the scope of this tutorial.) To help mitigate this problem, Screen has features that allow you to monitor a window for silence--useful for knowing when a compile has finished, for example--or activity--useful for knowing when someone is finally talking on your favorite IRC channel, for example.
To start or stop monitoring the current window for 30 seconds of silence, type C-a _; to start or stop monitoring a window for activity, type C-a M.
Making it go away
As you've seen from the section on detaching and reattaching, screen is not easy to kill. You can't just close its containing terminal. (Actually, you can, but then all those processes are still running on your system, ready to be reattached.) There's no "quit" key in the How do you fully exit screen?
To my knowledge, the only way to do this nicely (i.e. without sending nasty signals to processes that don't deserve it) is to close each of your screen windows. Exit whatever programs or shells each is running, and they will go away. When the last program running inside screen is exited, screen itself will go away.
Other stuff
Screen does much, much more than I've described above, but it's enough to get you started. You can type C-a ? for a terse list of commands available while you're in screen. The screen man page has a wealth of info, too. And here are some web resources, if you have questions:
Yahoo GNU Screen Group
A fairly high-traffic mailing list for screen. Many knowledgable people hang out here.
Sven Guckes' Screen Page
Another overview page; a good deal more terse than this one.
The official screen home page
No explanation needed here. The page is surprising sparse on details.
The screen man page
Nicely formatted and separated by section. Good reference. Be sure to check out the sections on using .screenrc to customize screen.
Screen FAQ
Not for newbies, but a good resource to dig into for troubleshooting.