(Brief) Intro to screen

Version R2

CONTENTS

  1. What is screen?
  2. Basic usage
    1. Starting screen
    2. Adding new windows
    3. Switching windows
    4. Killing windows
    5. Exiting screen
  3. Neat-0 usage
    1. Copy and Paste
    2. Digraphs
    3. Locking your session
    4. Detaching/reattaching sessions
  4. More, more, more

1. What is screen?

screen is a full-screen, text-based window manager. It multiplexes up to 10 virtual terminals onto a single physical terminal (or terminal emulator, or Linux VT).

It is quite possibly the ultimate power tool for the hardcore who eschew X (with default configs all around, you can have 60 foreground processes on a single Linux box without ever invoking X), or for when you have terminal-only access to a machine (for whatever reason).

^Back to TOC

2. Basic usage

This section covers the core functions of screen. You should be able to get started using it almost immediately.

^Back to TOC

2.1 Starting screen

There's not a lot to starting screen. Simply type screen at the command line. You may or may not see a splash screen, depending on how the program was compiled. Then, you will be back at the command line as if nothing had happened.

When you first start screen, it may complain about a termcap entry being too long. I've never seen this error before, but it doesn't actually seem to affect anything.

But something has happened. You're now inside screen, even though you have no indication of it. screen tries to stay out of your way as much as possible, so it doesn't display anything once it is up and running (unless you ask it to).

screen assigns a number and name to each window. Window numbers simply increment as you add new windows. Window names default to the name of the program running in the window (but we'll see how to change that later). You are in the first window, which is numbered 0 and is named after whichever shell you are running.

screen passes all keystrokes through to the running program except C-a (Control+'a'). C-a is the command prefix for screen (that is, it indicates that the next keystroke will be a command to screen, rather than the program running in the current window). For instance, we can find out how many windows we have and their names by typing C-a w (for window list). You should see something like this on the last line of your teminal:

[mdxi@foot mdxi]$ 0* bash2 mdxi]$
screen has the ability to put its status information in the titlebar of its terminal window if you are running it under X. If you see nothing on the bottom line of the window, check the titlebar.

You can see that screen temporarily overwrites part of the bottom line in reverse video to give you feedback at certain times. The overlay goes away after about 2 seconds or as soon as you hit a key. Here is what this bit of text tells you:

The emacs users are now wondering how they'll ever get to the beginning of a line, since screen claims C-a as its own. The answer is C-a a which sends a literal C-a to the app running in the current window.

^Back to TOC

2.2 Adding new windows

There are several ways to add a new window. You can type C-a c, which will create a new window running your login shell. You can accomplish the same thing by typing screen while screen is already running and it will create a new window rather than invoke a new instance of itself (no recursive screens! bad programmer!).

Do a C-a c followed by a C-a w and you should see this at the bottom of your term:

0- bash2 1* bash2

The new window automatically became active. Now do the same keystrokes again.

0 bash2 1- bash2 2* bash2

Now you have three windows and the newest (window 2) is active. But notice that window 1 now has a '-' following it where window 2 has the asterisk, and notice that in the previous example window 0 had this marker.

The hyphen tells you which window was previously active. This becomes helpful later on.

There is yet another way to create new windows, and this method is my preferred one. At a command line (in any window), type screen [program_name] and a new window will be invoked, already running the specified program. Try this:

[mdxi@foot mdxi]$ screen vi test.txt

vi will start up as expected, but do a C-a w and you'll now see this:

~ ~ 0 bash2 1 bash2 2- bash2 3* vi
^Back to TOC

2.3 Switching windows

Having multiple windows isn't much use if you can't switch between them. Luckily, there are several ways to do this. Each method does something slightly different, and they're all designed to minimize hassle.

The most straightforward method is C-a [number] where [number] is the number of the window you want to go to. Type C-a 1 followed by C-a w. You should see this:

0 bash2 1* bash2 2 bash2 3- vi

There are two related commands which let you cycle through the window list: C-a n and C-a p. As you might expect, C-a n will always take you to the next window in the list and C-a p always goes to the previous window.

For example, if you type C-a n now, window 2 will become active; if you type C-a p, window 0 will become active.

Finally we have C-a C-a which flips between the currently active and previously active windows.

So if you hit C-a C-a twice, you'll flip to window 3 and then back to window 1 (assuming you are continuing from the previous screenshot).

Remember: C-a p goes to the previous window in the window list and not the previously active window! To get to the previously active window, use C-a C-a.
^Back to TOC

2.4 Killing windows

There are two ways to kill windows.

The first way is to use the C-a K (kill window) command. This will remove the window from the list and forcibly terminate whatever program(s) were running in it. Please note that's capital 'K', not lowercase 'k'. I think this is because screen doesn't ask if you *really* mean it; it follows orders without question (as a good Unix program should).

Right now a C-a w should show this:

0 bash2 1* bash2 2 bash2 3- vi

Type C-a K and you get this:

0 bash2 2 bash2 3* vi

Window 1 is gone. Window 3 has become active again, and you no longer have a previously active window. If you create a new window right now, it will become window 1 rather than window 4.

The other way to kill a window is to terminate whatever program was running in it when it was created. Switch to window 2 and type exit to kill that shell session, then look at the window list again:

0 bash2 3* vi
^Back to TOC

2.5 Exiting screen

To terminate your screen session, simply kill all windows. screen wil die along with the last window, dropping you back to your original shell.

^Back to TOC

3. Neat-0 usage

Now it's time to cover some of the (even) cooler things that screen can do.

^Back to TOC

3.1 Copy and Paste

screen lets you copy and paste between windows (or in the same window, of course) even if you're not running under X. To start a copy, type C-a [. You'll see something like the following on the status line:

Copy mode - Column 17 Line 1(+1024) (80,24)

At this point you're free to move the cursor anywhere on the screen. When you get it where you want your selection to begin, press ENTER. The status line will echo

First mark set - Column 17 Line 1

Then move the cursor to the end of your selection, which will be shown in reverse video, and press ENTER again.

Copied 122 characters into buffer

Now simply move to where ever it is you wish to insert the copied text and type C-a ]

^Back to TOC

3.2 Digraphs

screen has, since its inception, been principly authored by Germans. Thus it natively includes support for digraphs. To generate one, type C-a C-v and you will be prompted to enter the characters making up the digraph. Here's a list of the ones I know:

KeyDigraph
[letter] + "Umlaut (ä, ë, ï, ö, ü)
[letter] + ’Accent acute (á, etc.)
[letter] + `Accent grave (à, etc.)
[letter] + ^Circumflex (â, etc.)
[letter] + ~Tilde (ñ, etc.)
(C | c) + ,Cedilla (Ç, ç)
(O | o) + /Null (Ø, ø)
(A | a) + oAngstrom (Å, å)
(A | a) + (E | e)AE ligature (Æ, æ)
s + sGerman sharp s (sz ligature) (ß)
^Back to TOC

3.2 Locking your session

Now that you are a master of using screen to manage your work efficiently, you certainly don't want to be bothered to shut everything down and set it all back up everytime you are called away from your desk. Well, as it turns out, there's no reason to do so.

By typing C-a x you can lock your session so that your terminal becomes inaccessible until you come back and type in your password.

Screen used by mdxi <mdxi> Password:

This is what you will see. My name is shown twice because my username and my real name (from the GECOS field) are the same. Just type your password and your session will be unlocked.

Depending on what version of screen you are using, or how it was compiled, you may be asked for a temporary password to use (in place of your system password) before locking the screen.
^Back to TOC

3.3 Detaching/reattaching sessions

screen even has a facility which allows you to move from one terminal to another and leave your sessions intact and running on the remote host. In fact, screen automatically detaches itself if you lose your connection to the host. All you have to do is reconnect, reattach your session and pick up right where you left off. This is, IMHO, the single coolest thing about screen, and it's very easy to do.

To manually detach a session, simply type C-a d and you will be dropped back to your login shell with a message like this:

[detached] [mdxi@foot mdxi]$

No matter if you detached manually or through a lost connection, the procedure for reattaching is the same. First, if you're not sure that you have a detached session, you can check by typing screen -ls:

[mdxi@foot mdxi]$ screen -ls There is a screen on: 15938.pts-37.foot (Detached) 1 Socket in /tmp/screens/S-mdxi. [mdxi@foot mdxi]$

Then just enter screen -r and you will be reattached to the session. Everything should be just as you left it; no programs should have terminated from loss of a controlling terminal.

^Back to TOC

4. More, more, more

screen does a lot more than the things illustrated here.

For example, in the earlier examples you had 3 windows titled "bash2" (or whatever your shell is called). To avoid confusion you can rename windows by using C-a A (not to be confused with C-a a or C-a C-a :). After entering the command key you will be prompted for a new name for the current window.

screen has a concise online help system which lists about half again as many commands as covered in this document. It is accessed by typing C-a ?.

Finally, screen has a full set of "colon commands" which are commands typed out after entering the C-a : command. In fact, the command keys shown in this guide call the colon commands. You can create new bindings for colon commands which don't have them or even rebind the existing ones to new keys of your liking. All this, and much much much more is covered in the screen manpage, which is where you should go look now if you still want to know more about how to use screen.

^Back to TOC