Screen is a terminal multiplexer and serial terminal. It has some useful features including the ability to lock, detach, and reattach a running session. tmux is also another terminal multiplexer offering similar features.

Quick Usage[edit | edit source]

Create a new screen with custom screen name:

$ screen -S test

This will start a new shell instance inside screen. You can see your current screen session by running

$ screen -ls
There are screens on:
        30922.test      (Attached)

Any jobs you run in this screen session can persist even if you log out if you detach from this screen session. Detach from a screen session by hitting Ctrl+a, d. This key combination is written as C-a d in the man pages. If you list the screens running again, you can see that the screen is detached.

$ screen -ls
There are screens on:
        30922.test      (Detached)

You may reconnect to detached screens by their partial name or PID number.

$ screen -r 3     # by the partial (but unique) PID number
$ screen -r 30922 # or by the full PID number
$ screen -r t     # by the partial (but unique) screen name
$ screen -r test  # or by the full screen name

Scroll back in a screen terminal by hitting Ctrl-a [ and then navigate with one of the following keys in the table.

Key Movement
h Move the cursor left by one character
j Move the cursor down by one line
k Move the cursor up by one line
l Move the cursor right by one character
0 Move to the beginning of the current line
$ Move to the end of the current line.
G Moves to the specified line (defaults to the end of the buffer).
C-u Scrolls a half page up.
C-b Scrolls a full page up.
C-d Scrolls a half page down.
C-f Scrolls the full page down.
/ Search forward
? Search backward

Tips & Tricks[edit | edit source]

Rename Screen Session[edit | edit source]

In a screen terminal, enter Ctrl-a :sessionname followed by the new name.


# screen -ls
There are screens on:
        8570.test       (Attached)
## Hit Ctrl-a :sessionname Hello
# screen -ls
There are screens on:
        8570.Hello      (Attached)

Lock a Session[edit | edit source]

You can use screen to lock the virtual terminal by using the Ctrl-x key.

Running a command detached[edit | edit source]

To run a command in a screen session in the background using the -dm flag to specify the command.

~ % screen -S test -dm sleep 60
~ % screen -ls
There are screens on:
        353.test        (Detached)

For more complex commands, you may want to wrap the complexity within sh -c. For instance, to use && or to do pipe redirections, enclose it within a subshell.

~ % screen -S test -dm sh -c "sleep 60 && echo hello"
~ % screen -ls
There are screens on:
        353.test        (Detached)

Running screen through su[edit | edit source]

If you are running screen not directly through the terminal (such as through su), you will get the following error message:

$ screen 
Cannot open your terminal '/dev/pts/0' - please check.

You can remedy this using script:

$ script /dev/null
$ screen  # Rerun script while in a typescript session

Serial Console[edit | edit source]

You can use screen as a serial console as well.

screen /dev/ttyS0 <baud>[,<options>]

Common baud rates are 9600 or 115200.

Configuration[edit | edit source]

Scrollback Buffer[edit | edit source]

Scrollback buffer can be changed by editing .screenrc. Define the following line with the number of lines to scroll back.

defscrollback 5000

Miscellaneous[edit | edit source]

Disable Visual Bell[edit | edit source]

To disable the visual bell for the current screen instance, hit:

Ctrl+a Ctrl+g

For a permanent fix, put the following into the ~/.screenrc file

vbell_msg "bell: window ~%" # Message for visual bell
vbellwait 2 # Seconds to pause the screen for visual bell
vbell off # Turns visual bell off

Force Background Redraw[edit | edit source]

Screen will sometimes mess with any background colors set by programs such as vim. This will cause weird visuals in the terminal where background colors are only rendered where there is text.

You can force screen to redraw the entire area such that the background colors are preserved by putting the following in your .screenrc file:

defbce on