screen allows you to connect to a terminal session from arbitrary places. For example, you can start a screen session on your desktop machine while connected from home and set a command running, then go to the office and connect to the same session. Your command-line history etc is fully available, it's exactly as if you took the xterm session with you. You can also reconnect to a session after losing the network connection from home, and soon.
Before starting, there's one default setting you should change. Edit your .screenrc file (it may or may not already exist, depending on your Linux installation) and add the following line to the end:
escape "^Xa"
type it exactly as it is written, that's shift-6, capital-X, not control-X. It means you will use control-X later, for various purposes.
Now start a screen session. We'll skip straight to the interesting bit of naming screen sessions, so try this:
screen -S myscreen
(note the 'S' is capital). You will see what looks like a new terminal prompt, exactly as if you had just logged in and changed to the current directory. Do something, such as ssh into a different machine and start editing a file with vi. Then detach from the screen session by typing ^X^D (that's really a control-X, control-D this time!). Your vi session dissapears and you are thrown back to the machine you issued the screen command from. Your vi session still exists on the othe machine, leading an independant life. You can return to it by typing
screen -r
et voila! Providing the network connection between the computer you started screen on and the one you ssh-ed into remains intact you can always resume your session.
So now you're connected to a screen session running vi, but what happens if you start this session from home and want to re-connect to it from your office? First, resume your original screen session as above. Then, in your office (or just in another terminal window), log in to the same computer you issued the screen command from. You can see your screen sessions with screen -ls:
screen -ls
There are screens on:
11819.myscreen (Attached)
The '11819.myscreen' is the 'session-id'. number is unique to each screen session, the name 'myscreen' is the one you specified with the -S option when you first issued the screen command. The 'Attached' tells you that at least one other window is attached to the session, but you knew that already. You can't use screen -r to resume a session which is attached, you have to use a variation of the command:
screen -a -x 11819.myscreen
You should now have two terminal windows open, both in the same vi session. Type something in onw and it is reflected in the other. They are, to all intents and purposes, the same session.
Suppose you want to have several sessions, such as one for running vi, another for compilation, and another for something else. It's best to name them, using the -S option, so you can identify them in the output of screen -ls. Then you can either resume them by name:
screen -r session-id
or attach to them if they are already attached:
screen -a -x session-id
So, if you use a single machine (such as your desktop) to start all your screen sessions, you can attach to all your sessions any time you like.
The biggest gotcha is the one we covered in the beginning, specifying the escape character. By specifying it as ^X, no other application that uses ^X can work properly (unless you read the man pages for screen and figure it out for yourself). One such application is pine, so if you start pine in a screen session you will not be able to send mail, the ^X is intercepted by screen!
You can get round this by specifying a different character in the .screenrc, choose your own. The default is ^A instead of ^X, which is a pain for anyone who uses command-line editing!
The other main gotcha is that screen is terminal-oriented. If you have am 80x24 xterm and an 80x45 xterm connected to the same session, they may well get confused. The screen man pages have more about how to cope with this.
Finally, you may need to set the TERM environment variable after starting screen. By default, it is set to screen. This is useless, it knows nothing about command-line editing etc. Set it to vt220 if you want a more functional terminal.