GNU Screen

Be a cowboy console, use GNU Screen!

Multiply your productivity by using a terminal multiplexer.

Yes, I know. It also exists tmux but I flipped a coin and (by now ^:) chose GNU Screen.

Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).

Take a tour

Run all commands in this section to take a tour of basic screen usage.

Start with a simple screen customization, create a ~/.screenrc with the following content

shell -$SHELL       # Load shell profile
defutf8 on          # Always start `screen` with UTF-8 enabled (`screen -U`)
truecolor on        # Enable colors.
startup_message off # No welcome message
vbell off           # Turns visual bell off
defscrollback 32000 # Set a large scrollback buffer

To start screen, just launch

screen

Display help with CTRL-a ?.

All screen commands are prefixed by CTRL-a, written in the help as ^A.

You can give a name to the session

screen -S mySecondScreenSession
``

List running sessions

```shell
$ screen -ls
There are screens on:
        16704.mySecondScreenSession (Attached)
        16562.pts-3.server01 (Attached)
2 Sockets in /var/run/screen/S-user.

Detach from current session with CTRL-a d.

Attach to a running session

screen -x pst-3

Use case

Suppose you need to login as user1 in some host, then you can switch to user user2, user3userN.

As user1 start a named screen session and switch to user2

screen -S user2
su - user2

then detach with CTRL-a d and you are back to user1 session.

Repeat the analogous commands for user3, till userN.

You get a screen session for every user. Now you do not need to enter passwords again and if the connection is broken you can enter again as user1 then go to user2 with

screen -x user2

Tips

Scroll window

  1. Hit CTRL-a Escape
  2. Move up or down with arrow keys
  3. When you are done hit q or Escape

Multiple windows

Enter in some screen session or create a new one, for instance

screen -x multiWindowSession

Create a new window with CTRL-a c. Cycle through windows with CTRL-a n or even better with CTRL-a CTRL-a i.e. holding CTRL and hitting a twice. Close window with CTRL-d.

Split window

Split window with CTRL-a S. It creates a new region below. You can also do it via a command with CTRL-a : and then split. In command mode you can hit TAB to autocomplete. To split vertically instead do CTRL-a |.

Go to new region with CTRL-a TAB.

The region is empty, create a new window inside it with CTRL-a c.

Resize region by entering a screen command with CTRL-a :, then type resize and it will prompt for the number of lines. There are other commands like resize -5 to reduce region height by five rows, see Resize section on screen online manual.

This is very useful if you want a separated window where you launch tasks like running tests, watch files for changes, tail logs, etc.

Remove region with CTRL-a X. If there is only one region, this will do nothing. You can close the window with CTRL-d.

Remove dead screens

If there are dead screen session you can clean them with

screen -wipe

References

See also