screen — Terminal Multiplexer for Persistent Sessions

Practical guide to screen: start, detach and reattach sessions, split windows and keep processes running across SSH disconnects.

screen is a terminal multiplexer: it wraps several virtual terminals into a single session that you can detach (Ctrl+a d) at any time and reattach later. That makes it indispensable on servers – kick off a long build or backup over SSH and it keeps running even if the connection drops or you log out. This guide walks you through the commands for sessions, windows, split regions and the .screenrc.

Sessions

screen — Start a new unnamed session.

screen

screen -S <name> — Start a new named session.

screen -S project

screen -ls — List all active screen sessions.

screen -ls

screen -r <name> — Reattach to a detached session.

screen -r project

screen -r — Reattach to the only detached session (fails if multiple exist).

screen -r

screen -d -r <name> — Detach a session from elsewhere and reattach it here.

screen -d -r project

screen -x <name> — Attach to a session that is already attached (multi-display mode).

screen -x project

screen -d <name> — Detach a session remotely without reattaching.

screen -d project

screen -X -S <name> quit — Kill a session by name from outside.

screen -X -S project quit

Session Key Bindings (Prefix: Ctrl+a)

Ctrl+a d — Detach from the current session (session keeps running).

Ctrl+a d

Ctrl+a \ — Kill all windows and terminate the session.

Ctrl+a \

Ctrl+a : sessionname <name> — Rename the current session.

Ctrl+a : sessionname myproject

Ctrl+a ? — Show all key bindings (help).

Ctrl+a ?

Windows

Ctrl+a c — Create a new window.

Ctrl+a c

Ctrl+a A — Rename the current window.

Ctrl+a A

Ctrl+a " — Show a list of all windows and select one.

Ctrl+a "

Ctrl+a n — Switch to the next window.

Ctrl+a n

Ctrl+a p — Switch to the previous window.

Ctrl+a p

Ctrl+a <number> — Switch to window by number (0-9).

Ctrl+a 3

Ctrl+a Ctrl+a — Toggle between the current and last window.

Ctrl+a Ctrl+a

Ctrl+a k — Kill the current window (with confirmation).

Ctrl+a k

Ctrl+a w — Show a brief list of windows in the status bar.

Ctrl+a w

Split Regions

Ctrl+a S — Split the screen horizontally (top/bottom).

Ctrl+a S

Ctrl+a | — Split the screen vertically (left/right).

Ctrl+a |

Ctrl+a Tab — Move focus to the next region.

Ctrl+a Tab

Ctrl+a X — Close the current region (window keeps running).

Ctrl+a X

Ctrl+a Q — Close all regions except the current one.

Ctrl+a Q

Copy & Scrollback

Ctrl+a [ — Enter copy/scrollback mode. Navigate with arrow keys or vi keys.

Ctrl+a [

Space — Set the start of the selection (in copy mode). Press again to copy.

Space

Ctrl+a ] — Paste the copied text.

Ctrl+a ]

Ctrl+a Esc — Enter copy mode (alternative to Ctrl+a [).

Ctrl+a Esc

Ctrl+a h — Write a hardcopy of the current screen to a file.

Ctrl+a h

Ctrl+a H — Toggle logging of the current window to a file.

Ctrl+a H

Miscellaneous

Ctrl+a : — Enter command mode (type screen commands directly).

Ctrl+a : hardstatus alwayslastline

Ctrl+a i — Show information about the current window.

Ctrl+a i

Ctrl+a t — Show the system time and host information.

Ctrl+a t

Ctrl+a x — Lock the screen session (requires password to unlock).

Ctrl+a x

Ctrl+a Z — Reset the terminal (useful after garbled output).

Ctrl+a Z

screen -L -S <name> — Start a session with automatic logging to screenlog.0.

screen -L -S logged-session

Configuration (~/.screenrc)

defscrollback <n> — Set the scrollback buffer size (number of lines).

defscrollback 10000

startup_message off — Disable the startup splash message.

startup_message off

hardstatus alwayslastline — Always show the status line at the bottom.

hardstatus alwayslastline "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= %D %d.%m.%Y %c"

shell -$SHELL — Use the default login shell for new windows.

shell -/bin/bash

bind <key> <command> — Bind a custom key to a screen command.

bind | split -v

screen -t <title> <n> <command> — Auto-create named windows with commands in .screenrc.

screen -t editor 0 vim
screen -t server 1 ./run.sh
screen -t logs 2 tail -f app.log

termcapinfo xterm* ti@:te@ — Enable mouse scrolling in xterm-compatible terminals.

termcapinfo xterm* ti@:te@

Conclusion

screen keeps your work alive: a detached session keeps running in the background whether your SSH connection drops or you log out – screen -r <name> puts you right back, and screen -x even lets several people watch the same session. For everyday use screen -S, Ctrl+a d, screen -ls and screen -r cover most of it. If you only need to decouple a single command from the terminal, nohup is leaner; if you want more modern window management, mouse support and more active maintenance, take a look at tmux. screen is the older, less actively maintained classic these days – but it ships pre-installed on virtually every Unix system and does its job reliably.

Further Reading

  • tmux – modern terminal multiplexer with mouse support and active maintenance
  • nohup – decouple a single command from the terminal without a full multiplexer
  • bash – the default shell running inside every screen window