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), for when you have terminal-only access to a machine for whatever reason, or just for a more efficient working style.
2. Basic Usage
This section covers the core functions of screen. You should be able to get started using it almost immediately.
2.1 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, but something has happened. You’re now in window zero of your screen session, even though you may have had no indication of it.
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 is controlled, like emacs, via command keys which are preceeded by a prefix key. The default prefix for screen is C-a (Control+a). For instance, we can find out how many windows we have and their names by typing C-a w (Control+a followed by w, to invoke the window list).
You should see something like this at the bottom of your terminal:
mdxi@fornax:~$ ls Archive Desktop GNUCASH WANTED bin htbr BUILD Diary GNUstep code images no_carrier mdxi@fornax:~$ 0* bash x:~$
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 the bit of text above tells you:
- There is one window in this screen session, window 0.
- Window 0 is active (indicated by the asterisk).
- Window 0 is named “bash”.
Note: 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.
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.
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 also do this by typing screen while screen is already running; 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- bash 1* bash
The new window automatically became active. Now do the same keystrokes again.
0 bash 1- bash 2* bash
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 further notice that in the previous example window 0 had this marker. The hyphen tells you which window was previously active.
There is yet another, more useful, way to create new windows. At a command line (in any window), type screen [program_name] and a new window will be invoked, running the specified program, without an intervening shell process. Try this:
mdxi@fornax:~$ screen vi test.txt
will start in a new window. Do C-a w and you’ll now see this:
~ ~ 0 bash 1 bash 2- bash 3* vi
2.3 Switching windows
Having multiple windows isn’t much use if you can’t switch between them. 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 to switch to window 1 and you’ll be back at a command prompt. Do C-a w and you’ll see:
0 bash 1* bash 2 bash2 3- vi
C-a n and C-a p let you cycle through the window list. As you might expect, C-a n takes you to the next
window in the list and C-a p goes to the previous window. If you type C-a n now, window 2 will become
active. if you type C-a p now, window 0 will become active.
There is also C-a C-a which flips between the currently active window (marked by an asterisk) and the previously active window (marked by a hyphen). 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.
2.4 Killing windows
There are two ways to kill windows. The first 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.
Warning: the kill-window function asks for confirmation before taking effect, but in older versions of screen the command was bound to C-a K (uppercase K) and did not ask for confirmation.
Switch to window 1 if you’re not already there, so that C-a w shows something like 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, the previously active window 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. The window list now shows:
0 bash2 3* vi
2.5 Exiting screen
To terminate your screen session, either kill all windows and screen will die along with the last window, or use the C-a \ command, which will ask for confirmation to kill all windows and exit.
3. Slightly Advanced Usage
Now it’s time to cover some of the even cooler things that screen can do.
3.3 Detaching/reattaching sessions
Screen allows you to move from one terminal or machine to another and leave your session intact and programs running on the host where you started the session. In fact, screen automatically detaches itself if you lose your connection a session. Once detatched, you can reattach your session at any time 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, type C-a d and you will drop back to your original shell with a message like this:
No matter if you detach manually or through a lost connection, the procedure for reattaching is the same. If you’re not sure if you have a detached session or not, you can check by typing screen -ls. If there is a session you’ll see:
mdxi@fornax:~$ screen -ls There is a screen on: 31960.pts-0.fornax (Detached) 1 Socket in /var/run/screen/S-mdxi. mdxi@fornax:~$
Just run 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.
If, for whatever reason, screen complains and you aren’t allowed to reconnect to the session (like a disconnect that wasn’t caught by screen, so it thinks it’s still attached, or you forgot to detach from work before coming home), run screen -DD -r to detach it and attach to your new login.
Tip: you can use screen -DD -r as a login shell. If there is no existing session, it will create
3.2 Multiview/Sharing a session
If a screen session is started with -x as an argument, then it becomes possible for the same user to run screen -x again elsewhere and control the same session from two places.
You might use this if you move between several machines frequently and don’t want the hassle of constantly detaching and attaching sessions. It can also be used to let two people (logged in as the same user) control the same program, or let one watch the other, but the original session must be started with -x so there’s no worry of being spied upon.
3.3 Copy and paste
Screen lets you copy and paste — within a window, or between windows — 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 and 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 move to the window and location of your choosing and insert the copied text with C-a ].
3.4 Renaming windows
Sometimes a process may give itself a lengthy window title, which affects its name in screen. Sometimes you may just accumulate a lot of shell processes and wish to uniquify them. Sometimes you may just want to rename a window. Do C-a A and you’ll be prompted for the new title.
screen has, since its inception, been principly authored by Germans, so 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:
(?, ?, ?, ?, ?)
acute (?, etc.)
grave (?, etc.)
| c) + ,
| o) + /
| a) + o
| a) + (E | e)
ligature (?, ?)
sharp s (sz ligature) (?)
If your locale is set correctly, the desired character will be entered at the cursor.
3.6 Online help
To see more commands available to you, or just to refresh your memory, you can always check screen’s built-in quick reference page. Just do C-a ? to call it up.
For in-depth help, or if something isn’t working like you think it should, refer to the manpage, which is authoritative.
4. Even More Advanced Usage
4.1 Colon commands
All the commands which are bound to the keys we’ve been discussing can also be accessed via their full names, along with many other commands we’ve not discussed and even more which do not have key bindings by default. I call these “colon commands” because the way to run them is to do C-a :, which will call up a prompt where you can type the command to be executed. For instance, the colon command for C-a : is colon (which is sort of a viscious cycle :-), and there’s the bind command is used to bind free keys and rebind existing ones.
There are literally dozens. A full listing can be found in the manpage.
4.2 The dotfile
Screen’s dotfile (.screenrc) is simply a list of colon commands which will be executed at startup. This is a very handy thing, as you can configure things to your taste and create a custom startup environment, all in the same place. Here’s my .screenrc:
# rebindings bind E screen -t emacs emacs -nw bind W screen -t w3m w3m ~/.w3m/bookmark.html # default windows screen -t fornax screen -t mutt mutt screen -t emacs emacs -nw screen -t w3m w3m ~/.w3m/bookmark.html
The lines beginning with pound signs are comments, and are ignores, as are blank lines. The second line binds capital E to a new screen (new window) with a title of “emacs” running the command “emacs -nw”. The third line does something similar. The last four lines are my initial set-up: 4 windows — each with
set titles — containing a shell, my mail, an editing session, and a web browsing session, respectively.
My .screenrc is pretty simple, but they can be very complex. The screen package comes with samples for both global and user-level dotfiles, so check them out (Debian users will find them in /usr/share/doc/screen