Catalog / GNU Screen Cheatsheet

GNU Screen Cheatsheet

A quick reference guide to using GNU Screen, a terminal multiplexer. Learn how to manage multiple terminal sessions within a single window, detach and reattach sessions, and customize your screen environment.

Session Management

Starting a Screen Session

screen - Starts a new screen session.

Example:

screen

screen -S session_name - Starts a named screen session.

Example:

screen -S my_session

screen -d -m command - Starts a detached screen session, running command.

Example:

screen -d -m ./my_script.sh

Detaching and Reattaching

Ctrl+a d

Detach the current screen session. The session continues to run in the background.

screen -r

Reattach to a detached screen session. If multiple sessions are running, it attaches to the first available.

screen -r session_name

Reattach to a specific named screen session.
Example:

screen -r my_session

screen -ls or screen -list

List all active screen sessions. Provides session IDs or names for reattaching.

Ending a Screen Session

exit or Ctrl+d - Exit the current shell in the screen window. If this is the last window in the session, the session will terminate.

Ctrl+a k - Kill the current window within the screen session. Confirm the kill to terminate the window.

Window Management

Creating New Windows

Ctrl+a c - Create a new window within the current screen session. This opens a new shell prompt.

Ctrl+a :screen command - Create a new window and execute command in it. The window automatically closes after the command completes.
Example:

Ctrl+a :screen top

Switching Between Windows

Ctrl+a n or Ctrl+a Ctrl+n

Switch to the next window in the session.

Ctrl+a p or Ctrl+a Ctrl+p

Switch to the previous window in the session.

Ctrl+a number

Switch to a specific window by its number (0-9).

Ctrl+a '

Prompts for a window number or title to switch to.

Ctrl+a Ctrl+a

Toggles between the current and previous window.

Window Information

Ctrl+a w - Display a list of all windows in the current session, indicating the current window with an asterisk.

Ctrl+a A - Rename the current window. This is useful for quickly identifying different tasks running in each window.

Ctrl+a t - Display the current time and system load average in the current window.

Advanced Features

Screen Splitting

Ctrl+a S

Split the current window horizontally into two regions.

Ctrl+a |

Split the current window vertically into two regions.

Ctrl+a tab

Switch the input focus to the next region. (Note: May be Ctrl+a Ctrl+i on some systems).

Ctrl+a Q

Remove all regions except the current one.

Ctrl+a X

Kill the current region. Requires confirmation.

Copy and Paste

Ctrl+a Esc - Enter copy mode. Use arrow keys to move the cursor.

Press Space to begin selection, move the cursor to the end of the text you want to copy, and press Space again to complete the selection.

Press Ctrl+a ] - Paste the copied text into the current window.

Scrolling

Ctrl+a [

Enter scrollback mode. Use arrow keys, Page Up, Page Down, Home, and End to navigate the scrollback buffer.

Esc

Exit scrollback mode.

Configuration

Configuration File

Screen’s configuration is managed through the ~/.screenrc file. This file allows you to customize keybindings, startup behavior, and other settings.

Example:

# Set a default window name
startup_message off
dedicated_cpu on

#bindkey ^k kill

# Hardstatus setup
hardstatus alwayslastline
hardstatus string '%{wk} %?%-w%?%{r}(%{W}%n*%t%?)%{k}%+w %=%{g} CPU:%l %{b} %H'

Common Configuration Options

startup_message off

Disable the startup message.

vbell off

Disable visual bell (flashing screen).

bindkey key command

Bind a key combination to a screen command. For example:

bindkey ^k kill #bind Ctrl+a k to kill the window

defscrollback size

Sets the default scrollback buffer size for new windows. Example:

defscrollback 10000

caption always

Always show caption line at the bottom of the screen.