1 Comment

GNU Screen Crash Course

GNU Screen is an application used to split a single terminal session into several processes, seeing any number of them at the same time. Like Vim, it has a bit of a learning curve, so here’s a crash course on how to control screen, focusing on what I’ve found are the most commonly used features: windows, regions, and copy mode.

Controlling Screen

Once screen is invoked (with the screen command on a terminal), it is controlled by pressing a command key sequence, then typing a letter which has been bound to a command, or ”:” followed by the full command. The default command key sequence is to hold the control button and press ‘a’. This is expressed in screen configuration files and documentation (and the remainder of this document) as ^a. Here’s an example:

  • ^a :screenlock (type ’:screenlock’ after releasing ^a) Locks the screen session, requiring user to enter password to resume
  • ^a x (press ‘x’ after releasing ^a) By default, ‘x’ is bound to :screenlock, making this a much faster way to
    lock screen

One common source of confusion is that terminal scrolling can be stopped by pressing ^s. This is useful to stop output from scrolling by on the display (while watching log output, for example), but doesn’t get much use in every day activity, and is generally unknown to many users. Due to the ’s’ key’s proximity to the ‘a’ key, it is very easy to accidentally activate this, and apparently break your terminal session if the command key sequence is mistyped. To resume scrolling, and reset your terminal back to normal behavior, press ^q.

One common complaint many users have about screen is that ^a collides with commands in other programs. For example, in bash, ^a moves the cursor to the beginning of the line. The makers of screen accounted for this and built in a shortcut: ^a a will send a ^a key sequence to the process running in screen.

Windows

Each process that is run in screen lives in a window, and screen allows you to have as many windows as you want. By default, when a window is created, it executes your default shell. Windows are both numbered and titled, with window numbering starting at zero.

  • ^a c Create a new window
  • ^a w Show a list of open windows
  • ^a n Switch to next window
  • ^a p Switch to previous window
  • ^a [0-9] Switch to window by specifying its number. For example, ^a 4 will switch to window 4
  • ^a ^a Switch to the previously focused window (useful for toggling between two open windows)
  • ^a :number N Move the window to become number N. For example, ^a :number 2 will move the current window to position 2.
  • ^a :title NewTitle Change the window’s title to ‘NewTitle’

Regions

Screen uses “regions” to view the contents of windows. By default the entire display is consumed by one main region, but it can be split into multiple regions to see several windows at the same time.

  • ^a | Split the current screen region vertically
  • ^a S Split the current screen region horizontally
  • ^a [tab] Switch focus to next screen region
  • ^a X Kill the current screen region
  • ^a Q Kill all screen regions except the current one

Copy Mode

Copy mode allows you to copy and paste text. It is also useful for going back through text that has scrolled off the screen and is no longer visible. While in copy mode, move the cursor with vim-like movement commands or with the arrow keys. Pressing ‘enter’ will set an endpoint for selection under the current cursor position. Pressing ‘enter’ again will set the other selection endpoint, copying the selected text to the copy buffer and leaving copy mode. Pressing ‘escape’ at any time will leave copy mode without writing anything to the copy buffer.

  • ^a [ enter copy mode
  • ^a ] paste copied text
  • ^a > write copy buffer contents to the screen exchange file (/tmp/screen-exchange)
  • ^a < read the contents of the screen exchange file into the copy buffer

The screen exchange file can be very useful for transferring text between screen sessions or semi-persistently storing text while screen exits for use later (“semi-persistent” because the file lives in /tmp/, and will thus be removed if the system is rebooted).
By using xsel, copying text from screen’s buffer into the system clipboard is fairly easy. Full instructions on how to do this may be found here, but basically just write the screen copy buffer contents to file with ^a >, then move the contents of the file into the system clipboard by running xsel -ib < /tmp/screen-exchange. I will show how to map this to ^a b in the “configuring screen” section.

Detaching

At any time, screen can detach from the terminal and will continue to run as a background process. This detached screen session will survive if the owning user logs off, making it extremely useful for ssh-ing into a remote machine, starting a job in a screen session, detaching the screen session, logging off, and resuming the job later by reattaching the same screen session. This can also be a lifesaver if ssh-ing over an unreliable connection, because your session will be saved in the event that you unexpectedly disconnect from the remote host.

  • ^a d detach from the current screen session
    Invoke screen with screen -R to reattach a previously detached session. Read screen’s documentation for how to manage multiple detached sessions.

Configuring Screen

By modifying screen’s configuration file (~/.screenrc) the default behavior of screen can be changed and configured and commands can be created or remapped. In this file, lines beginning with ’#’ are comments and ignored by screen. Here’s a sample file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#remap the control key sequence:
#    this command says to use ^x as the command key sequence,
#    and map the command ^x y to send a literal ^x key sequence
escape ^xy

#skip the greeting message
startup_message off

# add a status bar to the bottom of the screen
#    it contains a list of all windows
#    and displays the current time on the right
caption always "%w%?%=%c"

#bind 't' key to the 'title' command
bind 't' title

#bind 'b' key to copy the contents of the screen copy buffer
#    to the system clipboard using xsel, as described above
bind 'b' eval writebuf 'exec /bin/bash -c "/usr/bin/xsel -bi < /tmp/screen-exchange"'