screen and screenrc

I would like to use GNU screen as a window manager.

By default screen seems like little more than a persistent shell (that you can resume even after logging out). By itself, this is incredibly useful if you wish to access the exact same terminal session from different locations.

For example, on the command-line you can run screen or start a named session, i.e.,

$ screen -S sessionname

This starts a new shell, which you can detach from by pressing Ctrl-a and then d, i.e.,

^A d

Ctrl-a is the default command-key, you press Ctrl-a followed by a screen command. This is abbreviated as ^A in the screen manual.

By default, you'll also detach from screen by simply closing your terminal.

You can list existing screen sessions, and then re-attach to a screen session as follows,

$ screen -ls
There are screens on:
        23762.this      (Detached)
        23814.that      (Detached)
2 Sockets in /var/run/screen/S-timwarnock.

$ screen -r that

In this case I've re-attached to a screen session named "that".

If a screen session is already attached, and you want to takeover the session, you can do the following,

$ screen -d -r sessionname

If desired, you can use the -D option which will detach and logout the other terminal. You can also use -R to re-attach to a session or create one if it doesn't exist.

$ screen -d -r
 ... detach the current attached terminal and then re-attach
$ screen -D -r
 ... and logout the other terminal
$ screen -D -R
 ... and if no session, create one
$ screen -D -RR
 ... and if more than one session, use the first one

I prefer the following command,

$ screen -dRR

This will connect to the first session, politely detach as needed, or create a new one if no screen session exists.

Once you're attached to screen, you can run multiple windows, split the screen multiple ways, and various other useful tricks. At the very least, here are the most common commands for everyday use:

^A ? - help
^A c - create new window, or screen -t tabname
^A A - rename screen window
^A k - kill a screen window, or ^D (or exit) to properly exit
^A " - window list, change windows
^A n - next window, or ^A space
^A p - previous window
^AA - switch to previous screen (toggle)
^A [0-9] - go to window [0-9]
^A esc - copy mode (for scrollback), ] or esc to abort

If you want to get more advanced, and see what screen can really do, you'll want to save a .screenrc in your home directory. Otherwise managing multiple windows and split-screens in a single screen session can be quite confusing.

Here is an example screenrc,

# ^A :source ~/.screenrc
bind , source "$HOME/.screenrc"

# no default message, no audible bell
startup_message off
vbell on

# more usable scrollback
scrollback 5000

# detach on hangup
autodetach on

# make bold work
attrcolor b ".I"
attrcolor i "+b"

# hardstatus and caption
hardstatus alwayslastline "%{= wk}%-w%{= Bw} %n %t %{-}%+w %-= %H [ %Y-%m-%d %c:%s ]"
caption splitonly "%{= wK}%-w%?%F%{= bw}%:%{= Wk}%? %n %t %{-}%+w %-= "

# resize
bind + resize +5
bind - resize -5
bind = resize =

# bindings
bind % screen -t top top

Using the above screenrc with a resized split-screen looks like this:

The important bits of the screenrc are the hardstatus and caption. The hardstatus can set the bottom line in a terminal, in this case a window-list highlighting the active window, the date and time, and the hostname of the machine running the screen session.

The hostname is also on my command-prompt, but it's useful for me to know the hostname running the screen session separate from wherever the current shell happens to be logged in.

The caption in this case is set to only display when the screen is split. You can split a screen multiple times. In this case the caption will appear underneath its respective window.

The commands to split a window are as follows,

^A S – split screen
^A Q – return to non split view
^A Tab - move to the next window in split-display
^A :resize [n|+n|-n]

In the above screenrc, I've bound custom commands for resizing, this way you simply hit Ctrl-A followed by + or - to resize the active window (or = to return to the default size).

To copy+paste within screen, you enter "copy mode" and can view the scrollback buffer. Once in copy mode you hit enter to start selecting, and enter again to end your selection.

^A esc - copy mode, or ^A ]
^A ] - to paste

If you're using screen to manage several windows and want to be alerted when something changes on a given window, you can use monitoring. This simply alerts you of activity on a given window, or silence in the case you were expecting activity.

^A M - monitor for activity
^A _ - monitor for silence (default 30 seconds)

Lastly, you can log a screen window or do a simple grab of the contents currently on display, i.e.,

^A h - screen-grab
^A H - logging
This entry was posted in bash, shell tips. Bookmark the permalink.