Table Of Contents

Previous topic

Compiling the Linux kernel

Next topic


This Page

Using Screen


GNU screen is a console screen manager. It is useful for managing multiple terminal sessions without having to open more consoles, windows, or remote sessions. Using a combination of keyboard commands, screen can open new screens in a buffer, split the screen, and cycle through them.

Especially useful is screen’s ability to detach from the shell that it was started from. For example, one could log into a system remotely, start screen, work, detach from the screen session, then reattach to it at a later time, in a different location, over a different connection. This is a great way to make sure you don’t lose work if you are remotely logging in over an unreliable connection.

This article will cover some of the most commonly used features of screen. For more information, see the man page, info page, screen’s built in help, or search Google.

Starting, Detaching, Reattaching, and Stopping Screen

To begin a screen session, simply run screen from a terminal. It will be easier to reattach to a session later if you name the session when starting. Use the -S parameter to do so:

screen -S name

where name is an identifier for the session that you make up.

Commands to screen, covered more in depth in the next session, start with Ctrl+a. To detach from a screen session, use Ctrl+a then Ctrl+d. Reattach by running:

screen -r name

Occasionally screen needs to force a detach before it can successfully reattach. This is done with:

screen -d -r name

The list of currently active sessions is given by:

screen -list

If you use screen regularly, you may want to put that command in your .bash_profile so that when you log in the active screens are shown. To quit a screen session, simply exit from all active windows. When there are no windows left, you will be dropped back to your standard shell session.

Useful screen commands

All commands in screen, for the most part, begin with Ctrl+a. To see the built in help, first press Ctrl+a then ‘?’. That is, type a question mark with shift+/. Listed in the help screen are several Ctrl+a commands. I have some of the more common ones below.

Command         Description
-------         -----------
Ctrl+a, ?       Show built in help
Ctrl+a, c       Create a new screen
Ctrl+a, Ctrl+a  Cycle through screens (just hold Ctrl down, type aa)
Ctrl+a, n       Cycle through screens (Next screen)
Ctrl+a, Ctrl+n  Another cycle through screens
Ctrl+a, S       Split the screen (note the capital S)
Ctrl+a, X       Close the split screen you are currently in (note capital X)
Ctrl+a, tab     Switch between split screens
Ctrl+a, d       Detach from screen session
Ctrl+a, [       Start copy (see below)
Ctrl+a, ]       Paste (see below)

Split screen is a little tricky. To get a split console, begin a screen session. Use the Ctrl+a, S command to create the split. Then Ctrl+a, tab to switch to the newly created split. Create a new session with Ctrl+a, c. When finished, exit from the split session shell and use Ctrl+a, X to close the split.

Copy and Paste

GNU screen has the ability to copy text in a session which can then be pasted into the same screen, or another screen that is in the screen buffer or in the same buffer as a split screen. To begin a copy, use Ctrl+[. With the copy started, move around the screen with the h, j, k, and l keys (same as for vim). Start a copy mark with the spacebar, move to the end of the copy, and use space again to copy the selection. Paste the copied selection with Ctrl+a, ].

Recording Sessions

Screen can be used to record shell sessions. Recording the shell can be extremely useful for logging purposes and auditing. It is also a good idea when implementing recording to automatically reconnect to sessions. This information originated from and


# Auto-screen invocation. see:
# if we're coming from a remote SSH connection, in an interactive session
# then automatically put us into a screen(1) session.   Only try once
# -- if $STARTED_SCREEN is set, don't try it again, to avoid looping
# if screen fails for some reason.
if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ]
  [ -d $HOME/log/screen ] || mkdir -p $HOME/log/screen
  sleep 1
  screen -RR && exit 0
  # normally, execution of this rc script ends here...
  echo "Screen failed! continuing with normal bash startup"
# [end of auto-screen snippet]


# support color X terminals
termcap xterm 'XT:AF=\E[3%dm:AB=\E[4%dm:AX'
terminfo xterm 'XT:AF=\E[3%p1%dm:AB=\E[4%p1%dm:AX'
termcapinfo xterm 'XT:AF=\E[3%p1%dm:AB=\E[4%p1%dm:AX:hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
termcap xtermc 'XT:AF=\E[3%dm:AB=\E[4%dm:AX'
terminfo xtermc 'XT:AF=\E[3%p1%dm:AB=\E[4%p1%dm:AX'
termcapinfo xtermc 'XT:AF=\E[3%p1%dm:AB=\E[4%p1%dm:AX:hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

# detach on hangup
autodetach on
# no startup msg
startup_message off
# always use a login shell
shell -$SHELL

# auto-log
logfile $HOME/log/screen/%Y%m%d-%n.log
deflog on