An introduction to GNU screen

If you want to use desktop real estate more efficiently or if you ever find yourself without the X Window System yet needing more than one terminal, the popular GNU screen utility may be the perfect solution.

Screen enables you to run multiple processes -- such as interactive shells, text editors, web browsers or e-mail and irc clients -- multiplexed within a single terminal. You may have tried achieving the same result with tabbed terminal emulators such as gnome-terminal or Konsole, but screen boasts a long list of advantages over tabbed terminals.

In fact, screen has so many features, its man page is more than 20,000 words long! Obviously, though this story provides everything you need to begin taking advantage of screen's power, it only scratches the surface of this unusual and versatile tool. You're encouraged to read the man page and to experiment. To quote the man page: "A weird imagination is most useful to gain full advantage of all the features."

It's likely screen is already installed on your Linux computer. If for some reason it's not, use your distribution's package management system to install it, or visit here to acquire and install the latest version.

Let's try out some basic screen skills. Open a terminal window and type screen at the shell prompt. Depending on how screen was compiled, you may see a welcome message. Otherwise, you will simply see a new shell prompt. If you receive the welcome message, press space or return to continue.

Screen is now running. It doesn't look like much yet because it has simply created a single window with a shell in it. You may use this window immediately. As you create more windows, screen assigns a number to each. This first window is number 0. Subsequently created windows will be numbered: 1, 2, 3, and so on.

Create a new window now by typing the screen command for create. All screen commands begin with control-a, which the Screen man page abbreviates to C-a, and are followed by an additional keystroke. For example, to create a new window, type: C-a c. For purposes of this exercise, please create two new windows with C-a c.

To see a list of windows, type C-a w. Notice that screen has used the last line of your terminal to display a list of windows. You can tell which window is active because it is marked with *. Another way to see the list of windows, is to type C-a ". A list of windows appears, and you can navigate among them by using the arrow keys or vi movement keys. Press enter to bring the currently highlighted window to the foreground.

Switching to the next window is easy. Type C-a n. Type the next command again and screen will switch to the next window. When you type C-a n from the highest numbered window, you will wrap around to window 0. To move the other way, switching to the previous window rather than the next, Type C-a p. If you have many windows, it may be easier to use C-a " than next and previous.

Now that you've played with navigating among your windows, you may feel that the default window titles aren't very helpful. Fortunately, they're easy to change. To set the title of a window, navigate to the window you'd like to change and type C-a A. (Note that you must use capital A. Lower case a has a different meaning.) Set the title of a window now, and then use C-a " to list your windows. Note that your new title appears in the list. It will also appear in the output of C-a w.

It's also possible to create a new window and title simultaneously. At a shell prompt inside one of your screen session's windows, type screen -t title to create a new window titled "title". You can also run a specific command in the new window and name the window by the command. For example, to create a new window running the lynx Web browser and titled "lynx", type screen lynx. Finally, you can combine these features and create a new titled window running a specific command: screen -t title command.

Screen supports copying and pasting between windows. This is especially useful when you're running screen without X Windows and don't have the scrollback provided by a terminal emulator. Screen has it's own scrollback buffer for each window, and you can use the copy feature to view it and select portions into a paste buffer. To enter copy mode, type C-a [. Navigate the scrollback with vi movement keys. (You can customize screen to use emacs style navigation if you prefer.)

Now you must tell screen what region to copy to the paste buffer. Position your cursor where you would like to begin, and press space. Next, position your cursor where you would like to end, and press space again. Finally, navigate to the window in which you wish to paste, and type C-a ].

Each program running in your screen windows is independent from the others. It's just as though you were using many separate terminal emulators. However, unlike with separate terminal emulators, screen offers a tremendously useful feature: you can detach the entire screen session leaving all shells and applications running. Then, when you reattach, you have your complete, intact screen session at your fingertips. That means you can take a long break or even ssh in from another workstation and still pick up right where you left off.

Detach now by typing C-a d. The screen session is now detached from the current terminal but is still running. If you log out, walk the dog, log back in, your session will be waiting for you. Simply reattach by typing screen -r.

Because reattaching to a screen session is very useful, you may want to automatically reattach to a detached session whenever you log in to your account. Please see the story "Automatically Reattaching to a Screen Session" to learn how.

When you terminate a program, screen automatically kills the containing window. For example, if window 4 contains an interactive shell and you exit from the shell, screen will kill window 4. When you've killed the last remaining window, screen terminates. Note that you can also kill windows with the kill command: C-a C-k.

You now possess the basic screen skill set. Screen boasts many more features such as dividing windows into regions, session locking, and collaborative session control (for, as an example, pair programming). For a detailed discussion, type man screen, and for immediate help with basic screen key bindings, type C-a ?.

Screen is highly customizable. Although it's possible to customize screen on-the-fly, it's usually preferable to make changes permanent via the $HOME/.screenrc file.

For system-wide changes that affect all users, you may edit the global screenrc, usually found in /usr/local/etc. The location of the global screenrc may be different on your system. If it is, use locate or consult your distribution's documentation to find it. By default, when screen starts, it will source the global screenrc and then the .screenrc in your home directory, in that order.

In your .screenrc, you can set options, bind functions to keys, and set one or more default windows when screen initializes. Below are examples of customizations you may wish to make.

I find control-a inconvenient. Instead, I instruct screen, via my $HOME/.screenrc, to use backtick (`) in place of C-a. If I ever need a literal backtick, I simply type ` twice. To make this change, I add a new line to my .screenrc that reads as follows:

escape ``

Note that commands in your .screenrc are listed one per line, and lines beginning with # are considered comments and are ignored by screen.

Many people prefer screen's visual bell to their emulator's visual bell. To turn on visual bell in screen, add this command to your.

screenrc:

vbell on

You can also set the message screen uses for visual bell (though "Wuff! Wuff!" is cute) by setting vbell_msg.

If you're converting to screen from a tabbed terminal emulator, you may miss having the visual cue of actually seeing your tabs and knowing which one is active. There is a way of achieving the same result in screen. The trick is to make the status line persistently visible. The command to add to your .screenrc is hardstatus. Here are the lines from my .screenrc that invoke hardstatus and set the status line to show the window list:

hardstatus alwayslastline

hardstatus string '%{= wk}%-Lw%{= KW}%50>%n%f* %t%{= dK}%+Lw%<'

Consult the string escapes section of the screen man page for information about configuring this line, including how to change the foreground and background colors of the various components of the status.

Now that you've seen some of what screen has to offer, you can imagine how useful it will be in your day-to-day work. Once you pass the initial learning curve, you will find it to be an indispensable part of your computing environment. For more help with screen, consult the screen man page, use C-a ?, and try screen -?.

This story, "An introduction to GNU screen" was originally published by LinuxWorld-(US).

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Related:

Copyright © 2006 IDG Communications, Inc.

IT Salary Survey: The results are in