Screen

Screen is a moderately complex program that emulates a terminal. The feature it is best known for is the ability to disconnect and reconnect to screen sessions, which very gracefully survive logoffs. Screen has many more useful features, which are beyond the scope of this guide. Please refer to the GNU Screen Project Page for more information.

If you do not like unnecessary windows open, or want to get the maximum out of your terminal session, screen will soon be in your numerous times.

= Getting started with Screen =

First you have to install the program (if it isn't already installed). This is as simple as

To start using Screen open a terminal and type Depending on the setup of Screen you may be greeted by a startup message, but Gentoo has this usually disabled in with the line startup_message off. More about those settings later.



The first sign of Screen successfully running can be discovered by typing which shows the list of terminals. At the moment there is only one. The key exits that window. Alternatively, use to list them briefly in the window's title bar.



To create a new terminal just type. The windowlist lists now two terminals, the old and the new one. Naming the open terminals helps keeping an overview. Therefore allows you to give your new terminal a name.

With for next or  for previous you can cycle through your open windows.

Detach and Reattach
One of the most useful features of GNU Screen is: If we close the parent terminal Screen continues running! It simply "detaches" and can be accessed again — without any effect on the tasks running. Which makes it ideal for working on remote machines.

To detach your open Screen session just type or simply close it by closing the terminal window.

We are now dropped back into the shell that we started with in the beginning. At first we want to check if there is really a Screen-session running. These are listed by

To access this one instance we re-attach with Voilà, here we go again!

This already explains the basic usage of Screen, and gives us a method to leave compiles running on a remote or local system without the need to keep a terminal open all the time.

Orphaned screen sessions
A remote Screen session is sometimes not properly detached when e.g. the local was killed or the net connection died. The session will show as attached when listing them with.

To attach to this session you can either use its session name or tell Screen to detach it first, which is the easiest way if there is only one session existing.

To attach a specific session out of a list returned by, like , where is e.g. , use  although you only have to type the first unique number, in this example typing  would attach to the  screen session.

Naming screen sessions
You can make reattaching to a running screen session easier by giving the session a unique name when you start it.

This will create a new screen session named. If you can create another screen session named. You can then select to either session by using the given name.

Resizing a window to fit your terminal
Sometimes you may connect to a screen from a terminal with different geometry from when the session was started. To resize a specific window you can use to "fit" the window to the current terminal.

If you know before you attach to a screen that you want to resize all windows to fit you current terminal you can use the option to.

= Quitting screen =

One way to close a Screen session is to all terminal sessions you are running inside, one after the other. After the last inside Screen you will get  as a confirmation that your Screen session has ended.

However, a simpler and faster way to do the same thing is to use Screen's command.

This will kill all programs running inside the current Screen session and exit the session. If you use this command frequently, it may be useful to create a keybinding in your configuration file:

See the "INPUT TRANSLATION" section of the Screen manual for details on how to bind function keys (e.g, F1, F2, etc.).

= Regions =



Similar to vim we can use a single terminal window for more than one visible task.

Screen can horizontally split into regions, each holding a different terminal. To open a new region one types (capital s, please!). To enter that newly created region we have to tab into it: Still nothing can be seen, but we can now cycle through our open terminals with  or. To close a region type. A region with focus can be resized with or  By default this changes the height by 3 lines. Alternatively, you can specify the height to an absolute number by going into Screen's command mode. makes the currently focused region 20 lines high.

= Scrolling back in screen =

To enter the scrollback mode press. When you do this, a notice briefly appears in the terminal's status-bar, which says "Copy mode...". Now, you can scroll up and down in the current terminal using the / keys or the and  commands. Although quite useful by itself, scrollback mode is even more powerful when combined with copy and paste.

If you use scrollback feature a lot and you are using URxvt, then you might want to set:

This enables you to use / to move back and forth in scroll buffer. Another frequent cause of using scrollback buffer is, when programs like Vim exit they leave their content on the window. To get rid of this behavior use:

Selecting text, copying, pasting
Copying works by selecting a start position and an end position for text to be copied. These are marked with the cursor's current position by pressing. Between two strokes the buffer can be navigated with ,,,, they work just as in the editor vim. On most machines you can also use the arrow keys as well if you are not familiar with vim movement commands. And for the fans of emacs this default behavior can be changed in .screenrc. Consult the man page for the wealth of other movement commands available.

The content of the buffer can then be pasted back into any other Screen window of the current session by typing.

Advanced copying
One can even access the file system to copy files into Screen, or concatenate selections in a Screen window into a file on the machine where Screen is running on. To copy a funny signature from a Usenet posting set Screen into copy mode with, select the text between two keystrokes and then set a buffer file in the current directory with. This file is written into (and overwritten if existing!) by.

Note that sets Screen in the command mode and you actually type in Screen's status-bar. If no buffer file is given, Screen uses a default, because this feature is primarily meant for exchanging data between Screen users on the same system.

= Command mode = Typing puts Screen into command mode and the status line at the bottom changes. Screen now accepts direct commands.

= Accessing open terminals =

In addition to cycling through open terminals using and, you can directly access terminals 0-9 with  where # is 0-9. (I.e. will switch to terminal 1;  will switch to terminal 8.).

If you spend a lot of time on the command line and have more than ten windows open, you can get better access to higher terminals than by adding this to your

Issue the command to re-source your settings.

Now opens the 10th terminal under Screen's control (if it exists).

Monitoring a Window for Silence or Activity


You may have multiple windows open with various task in each and would like to monitor different windows for activity or silence, like you have an IRC client in one window, and running in another, and are working on code in a third. You might want to know when is done or if someone speaks in IRC.

The command turns on monitoring mode for 15 seconds of silence in the current window, which triggers a notification in your terminal's status area. Repeat the command to switch off the monitoring.

The command sequence turns on monitoring for all activity in the current window. Repeat the command to turn it off.

Control a Screen within a Screen
Sometimes it can happen that you accidentally start screen from within screen or attach to a screen from within a screen. When you try to detach from the inner screen, you actually detach from the outer screen. The problem is that your first screen will catch the. To control a screen within a different screen you have to first hit and then do what you want to do with the other screen, without the.

Using this method, one can control any number of nested screen sessions within other screen sessions, you just have to keep adding an additional meta-command character for every nested screen instance. This can becomes a bear to keep all of them straight in your head. To overcome this, some people will set the escape sequence differently depending on the nested level of the screen session.

= Configuring Screen sessions with .screenrc =

The window opened last is the one that is left active after screen starts up. See in the man pages for to change the preselected window. When the command used to open the window ends, that window closes. The above syntax for runs the command, but leaves the window open when the program terminates.

You can specify any file to be used in place of by using the  option.

Changing the escape sequence
Sometimes you may want to change the default escape sequence to something different (E.g. nesting screen sessions).

If you want to have the backtick toggle between the two most recent windows as C-a C-a would, you should instead have something similar to the following in your :

This will bind the backtick character to and allow  to toggle windows. Note that this will clobber whatever was previously bound to. 'e' is chosen here as it is not used by default.

Please note that using the backtick can cause problems with mouse-scrolling, as the wheel's escape code can have a backtick in it.

Some programmers may find it quite inconvenient to use the backtick, so feel free to experiment with other characters or simply use the default setting.

Emacs uses for beginning-of-line. It is also the command key for Screen, which causes the problem of muscle memory impedence matching. This can be avoided with the following, which binds the command key of screen to.

is also a popular choice, although this will conflict with the transpose function. Also is the default for the Ratpoison Window Manager. (which is very Screen-like and works well with Emacs and Screen)

Tab-bar
A really nice thing is make Screen show a "tab-bar" with your open windows.

You can add this functionality by adding the following lines to your file.

Another one you could try

A very nice one is this one with hostname, centered tabs and redmarked active windows:

No clutter, hostname, load, centered clean (ONLY # and name) tabs, date, and time. Uses caption instead of hardstatus so tab-bar appears on every (split screen) region.

= Multi-user mode =

Useful for collaborative install sessions is the multiuser mode of Screen. Just execute to attach to a Screen multiple times.

= Quick walk-through =

Problems with screens being "frozen"?
It is easy to confuse, which a uses a capital 'S' with , which uses a lower case 's'. The upper case command causes screen to be horizontally split (that is, with one region on top of the other), while the lower case command causes the parent terminal to freeze (a relic of old terminals which might get "overloaded" with input and need some way to immediately stop screen changes). To unfreeze the parent terminal, use the command.

To remove this command, use the command sequence:. This command can be entered at the colon (':') prompt or in the screen startup file.

What happens is that Screen sends a (xoff) to the screen, which freezes all output. The screen command is bound to the  command. You achieve the same effect in most terminals running bash by typing. (Type to undo this effect)

You might have noticed that you can still navigate and use the other windows, and even create new ones after an accidental. To continue, use the (which is the  command). For a proper lock of the session simply type and only your password gives you access again.

You may also have a problem with which sends a suspend to the terminal. Type once again to resume and/or turn off altogether with the stty command.

'''It is also to be noted that when dealing with uppercase commands, you can't type the whole command (i.e ) in a sequence. You must first then '''

Problems starting screen on login?
Sometimes it's useful to have screen for all your terminal sessions (by default). When working on a remote server, that helps you not to lose work because your connection was dropped. There are two options for that: either use screen as your login shell or do exec screen from your .bash_profile, or .profile or .login (for tcsh).

Using screen as your login shell is actractive, but then you should tell screen to use a shell other that itself for new windows, so that screen doesn't start itself in an infinite loop. You do that by adding the following to your :

There are a couple of disadvantages to this approach:
 * 1) Many programs use the $SHELL environment variable when executing commands, so $SHELL really should be a shell.
 * 2) When started as a login shell, screen executes as though it had been called like, and that means that no matter how many times you login, you always attach to the same screen session.
 * 3) You may need to have important login stuff in your .bash_profile, such as setting locale or something, and this won't be executed if screen is your login shell.

The other approach, having screen be executed on login automatically is actually the canonical approach, with none of the above disadvantages. You just add the following to your login script (for whatever shell you use):

.login

Although this is the canonical way to start screen on login, the file in Gentoo has something which prevents this from working. Usually, the shells that screen starts on each new window are not login shells. That is the default behaviour of screen and the configuration files that come in the distribution. However, because not everybody uses screen in the same way, people on Gentoo have added a line on which makes screen always start shells as login shells. That line is:

This effectively creates an infinite loop if you start screen on your login script. Your login shell starts screen which starts another login shell, and so on, and so on. So, for this to work you have to change the shell either on your or by removing that line on. If you want to change that just for you, then add the following:

Which effectively sets the shell to its default.