# screen — Terminal Multiplexer for Persistent Sessions

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

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/screen/

<!-- PROSE:intro -->
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`.
<!-- PROSE:intro:end -->

## Sessions

`screen` — Start a new unnamed session.

```bash
screen
```

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

```bash
screen -S project
```

`screen -ls` — List all active screen sessions.

```bash
screen -ls
```

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

```bash
screen -r project
```

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

```bash
screen -r
```

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

```bash
screen -d -r project
```

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

```bash
screen -x project
```

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

```bash
screen -d project
```

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

```bash
screen -X -S project quit
```

## Session Key Bindings (Prefix: Ctrl+a)

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

```bash
Ctrl+a d
```

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

```bash
Ctrl+a \
```

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

```bash
Ctrl+a : sessionname myproject
```

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

```bash
Ctrl+a ?
```

## Windows

`Ctrl+a c` — Create a new window.

```bash
Ctrl+a c
```

`Ctrl+a A` — Rename the current window.

```bash
Ctrl+a A
```

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

```bash
Ctrl+a "
```

`Ctrl+a n` — Switch to the next window.

```bash
Ctrl+a n
```

`Ctrl+a p` — Switch to the previous window.

```bash
Ctrl+a p
```

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

```bash
Ctrl+a 3
```

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

```bash
Ctrl+a Ctrl+a
```

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

```bash
Ctrl+a k
```

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

```bash
Ctrl+a w
```

## Split Regions

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

```bash
Ctrl+a S
```

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

```bash
Ctrl+a |
```

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

```bash
Ctrl+a Tab
```

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

```bash
Ctrl+a X
```

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

```bash
Ctrl+a Q
```

## Copy & Scrollback

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

```bash
Ctrl+a [
```

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

```bash
Space
```

`Ctrl+a ]` — Paste the copied text.

```bash
Ctrl+a ]
```

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

```bash
Ctrl+a Esc
```

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

```bash
Ctrl+a h
```

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

```bash
Ctrl+a H
```

## Miscellaneous

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

```bash
Ctrl+a : hardstatus alwayslastline
```

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

```bash
Ctrl+a i
```

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

```bash
Ctrl+a t
```

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

```bash
Ctrl+a x
```

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

```bash
Ctrl+a Z
```

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

```bash
screen -L -S logged-session
```

## Configuration (~/.screenrc)

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

```bash
defscrollback 10000
```

`startup_message off` — Disable the startup splash message.

```bash
startup_message off
```

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

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

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

```bash
shell -/bin/bash
```

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

```bash
bind | split -v
```

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

```bash
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.

```bash
termcapinfo xterm* ti@:te@
```

<!-- PROSE:outro -->
## 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

- [GNU Screen Manual](https://www.gnu.org/software/screen/manual/) – official reference manual with all options and key bindings
- [Arch Wiki: GNU Screen](https://wiki.archlinux.org/title/GNU_Screen) – practical guide to configuration and day-to-day usage
<!-- PROSE:outro:end -->

## Related Commands

- [tmux](https://www.jpkc.com/db/en/cheatsheets/shell-system/tmux/) – modern terminal multiplexer with mouse support and active maintenance
- [nohup](https://www.jpkc.com/db/en/cheatsheets/shell-system/nohup/) – decouple a single command from the terminal without a full multiplexer
- [bash](https://www.jpkc.com/db/en/cheatsheets/shell-system/bash/) – the default shell running inside every screen window

