screen Cheatsheet - Terminal Session Manager Reference
Losing a compile or a long interactive process when SSH drops is always the same mistake: the program lives inside the network connection instead of beside it. screen and tmux both add a local session layer so work outlives the wire, and this sheet covers screen because it ships with essentially every distribution and macOS, and because its model is small enough to learn in one pass: screen -S name to create, Ctrl-a d to detach, screen -r name to reattach. The window and split groups cover creation, switching and vertical or horizontal division with Ctrl-a S and Ctrl-a |. Copy mode is where newcomers stall, because screen has no system-level selection: Ctrl-a [, mark the region, y to copy, Ctrl-a ] to paste. Pick tmux when you want plugins, scripted layouts or a shared session; pick screen when installing it is out of scope, or to take over a legacy session quickly.
Typical Use Case
For ops and backend engineers whose work outlives an SSH connection: long builds, tailing logs, training jobs, tunnels. screen predates tmux and is still installed everywhere, including minimal containers and embedded appliances where tmux is absent. This sheet covers session lifecycle (create, detach, reattach, kill), window and split management, and the copy-mode keys people only discover after they have already lost a scrollback buffer.
Session Management 8
screen -S namescreen -lsscreen -r namescreen -d namescreen -x namescreen -X -S name quitCtrl+a dscreen -RD nameWindows 8
Ctrl+a cCtrl+a n / pCtrl+a 0-9Ctrl+a ACtrl+a kCtrl+a wCtrl+a "Ctrl+a Ctrl+aSplit Screen 5
Ctrl+a SCtrl+a |Ctrl+a TabCtrl+a XCtrl+a QCopy Mode 6
Ctrl+a [SpaceEnterCtrl+a ]Ctrl+a EscPageUp / PageDownOther 5
Ctrl+a ?Ctrl+a :Ctrl+a HCtrl+a xscreen -dmS name cmdConfiguration (.screenrc) 6
startup_message offdefscrollback 10000term xterm-256colorescape ^Bbscreen -t editor 1 vimcaption always "%n %t"Parameter matrix
| Parameter | Effect | Example |
|---|---|---|
screen -dmS name cmd | Create a detached session running cmd, without occupying the current terminal | screen -dmS name cmd |
screen -RD name | Force-detach the remote session and reattach - the default way to get a session back | screen -RD name |
screen -x name | Attach to a session that is already attached, sharing one screen between users | screen -x name |
screen -X -S name quit | Quit a session from outside it, without attaching first | screen -X -S name quit |
Ctrl+a d | Detach while the session keeps running; not Ctrl+a k, which kills the window | Ctrl+a d |
Common pitfalls
Symptomscreen -r name fails with "There is a screen on: xxx attached".
CauseThe session is still attached to a terminal whose connection is dead, so screen considers it busy.
FixUse screen -RD name to force-detach and reattach; screen -x name if you want to share it.
SymptomYou meant to leave a job running in the background, but Ctrl+a k killed it.
CauseCtrl+a d detaches and keeps the process alive; Ctrl+a k closes the window and terminates it.
FixAlways detach with Ctrl+a d, and end sessions with screen -X -S name quit.
SymptomScrollback holds only a few hundred lines, so older output is gone.
CauseThe default scrollback buffer is small.
FixAdd defscrollback 10000 to ~/.screenrc.
SymptomCtrl+a | (vertical split) does nothing on some machines.
CauseVertical split needs screen 4.1 or newer; older builds only offer Ctrl+a S.
FixCheck with screen -v, and use Ctrl+a S for a horizontal split on old versions.
SymptomThe startup banner pushes your real output off the screen every time.
Causescreen prints startup_message on startup by default.
FixAdd startup_message off to ~/.screenrc.
Troubleshooting
1Recover a session that seems to be gone
screen -ls && screen -RD name-ls shows the session name and state; -RD force-detaches and reattaches even when someone else holds it.
2Reap sessions nobody is using
screen -X -S name quitDetach first with screen -d name if it is still attached, otherwise you kill whatever it runs.
3Watch the same output with two people
screen -x nameCombine with Ctrl+a S / Ctrl+a | splits so each person views half the screen.
Command Examples
The create, list, and reattach trio
screen -dmS build bash
screen -ls
screen -RD buildscreen -dmS name cmd creates a detached session running the command, screen -ls shows its state, and screen -RD reattaches. -d detaches immediately so the current terminal stays usable; -R falls back to -D and kicks any other attached client, which is exactly what you want on a shared box.
Output
There are screens on: 1.build (Detached) 1 Socket in /run/screen/S-user. 4582.build (Detached) 1 Socket in /run/screen/S-user.
Split the pane between a build and its log
# Run these in sequence inside a screen session:
# Ctrl+a S split horizontally (top/bottom)
# Ctrl+a Tab switch focus
# Ctrl+a X close the current regionRunning a build in one half while tailing logs in the other is the reason screen still exists. Note that Ctrl+a S splits horizontally (top/bottom) while Ctrl+a | splits left/right and needs screen 4.1+; X closes the current region, Q closes all but the current one.
Prefix key and scrollback in .screenrc
escape ^Bb
defscrollback 10000
startup_message off
term xterm-256colorPutting escape ^Bb in ~/.screenrc moves the prefix to Ctrl+b and clears the usual clash with Ctrl-a style bindings. defscrollback defaults to only a few hundred lines, so long build output gets truncated; 10000 is far more practical. After changing the prefix, every Ctrl+a below becomes Ctrl+b.
Scroll in copy mode and grab a log excerpt
# Ctrl+a [ enter copy mode
# PageUp / PageDown page through the history
# Space starts the selection, Enter copies it
# Ctrl+a ] paste into any shellScrolling back with the mouse wheel in screen often does nothing; copy mode (Ctrl+a [) is the real entry point. After selecting and pressing Enter the text lands in a separate buffer, and Ctrl+a ] pastes it into the current shell — the reliable way to lift a fragment out of long output.
Check for leftover sessions before quitting
screen -ls
screen -X -S build quit
screen -X -S deploy quitA detached session keeps its processes running. Make screen -ls a habit before you disconnect, so training jobs and compilers are not left burning CPU unattended. Use screen -X -S name quit to end one for real; Ctrl+a d or screen -d name only detaches.
Common Pitfalls
- screen -X -S name quit kills every process inside the session with no chance to save: running training jobs, in-flight builds and unsaved vim buffers all disappear with it. Inspect with Ctrl+a [ first, or run under nohup / inside tmux as a second layer.
- screen rescues you when SSH drops, but a real network failure can leave a detached zombie session. Reattaching with screen -RD keeps the old process running and attaches you to it, which can end up with two processes writing the same output.
- Ctrl+a is the terminal XON flow-control character, so during streaming output it swallows the following bytes instead of triggering the shortcut. It looks like "the key does nothing"; the fixes are stty -ixon or changing the prefix key.
- The default defscrollback is only a few hundred lines: long output cannot be scrolled to the top and copy mode cannot reach it either. Edit defscrollback in ~/.screenrc, but note the change only applies to newly created sessions.
- screen -r fails with "There is only one screen" when someone else has the session attached, which is a waste of debugging time. screen -RD is the one to memorise: -R reattaches, and -D kicks the stale client if that fails.
- screen and tmux share the same terminal prefix semantics with different meanings: Ctrl+a d does something entirely different in tmux. On a machine running both, sending one prefix while believing you are in the other shows up as "the shortcut does nothing" with no error at all.
Tips
- After SSH drops, the screen session survives; re-ssh and screen -r to resume — essential for remote work.
- Ctrl+a is the default prefix; change it in ~/.screenrc if it clashes with the terminal.
- screen is dated; for new projects prefer tmux (modern, plugin-supporting).
FAQ
How do I launch a command in the background under screen?
Use screen -dmS name cmd. -d detaches immediately so the command never occupies your current terminal, and -mS creates a session called name and runs cmd inside it. The session ends on its own when the command finishes and your terminal is returned at once, which makes it a good fit for crontab entries, CI steps, or anything that must not block. Do keep the name — it shows up in screen -ls and is the only thing telling you which job a stale session belongs to. To watch progress, screen -r name to attach, then Ctrl+a d to detach again without disturbing the running job.
I want every new screen session to come up pre-arranged and already inside vim. How?
Put screen -t editor 1 vim in your ~/.screenrc: -t editor names the window, 1 makes it window one, and vim is the command executed in it. Screen builds the window during startup, so you land inside vim the moment you attach, with no manual window juggling. Baking in the name has a bonus — paired with caption always "%n %t" the status bar always shows the window number and title, so you never lose track of which window you are in when several are open.
After changing the prefix key, do the old shortcuts still work?
Not directly — every command has to use the new prefix. escape ^Bb in the config file swaps Ctrl+a for Ctrl+b, and from that point the commands read Ctrl+b d, Ctrl+b c, Ctrl+b S. Screen keeps one escape hatch: pressing Ctrl+b twice still does what the old Ctrl+a Ctrl+a did, switching between the current and previous window. That double-press is a useful bridge while the new prefix still feels unfamiliar. While you are editing the file anyway, add term xterm-256color so the screen and host terminal agree on colour depth and a 256-colour session does not get downgraded.
What does a full pass through copy mode look like?
Five steps. Press Ctrl+a [ to enter copy mode, where PageUp and PageDown scroll back through history. Once you are at the right place press Space to start a selection and adjust it with the arrow keys or a mouse drag. Press Enter to copy the selected run and finish selecting. Then Ctrl+a ] pastes the most recent copy at the cursor. If you still need to return to normal command mode, press Ctrl+a Esc. The practical value is that this reuses screen own scrollback buffer, so text that scrolled off the visible pane can still be recovered for as long as the buffer retains it.
With many windows open, which switching method should I use?
Match the method to the intent. Ctrl+a n and Ctrl+a p step to the adjacent window, which suits casual sequential browsing. Ctrl+a 0-9 jumps straight to a number and is the fastest option while the window count is still small. Ctrl+a Ctrl+a toggles only between the two most recently used windows, which is all you need when you are watching two or three service logs at once — just press the prefix twice. Ctrl+a " opens an interactive picker listing every window, the way to go once you are past ten and the numbers no longer stick in your head. Ctrl+a w shows that same list without switching, handy for confirming where you are.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us