# GNU Screen Quick Reference

*Sessions, windows, splits, copy mode, and sharing*

> Source: GNU Screen Manual (gnu.org/software/screen) · MIT

## Session Management

### Create & List Sessions

```
screen                        # new unnamed session
screen -S work                # new session named "work"
screen -ls                    # list all sessions
screen -list                  # same as -ls
```

### Session Commands

| Command | Description |
|---------|-------------|
| `screen -S name` | Start new named session |
| `screen -r name` | Reattach to detached session |
| `screen -x name` | Attach to already-attached session (share) |
| `screen -d name` | Detach a remote session |
| `screen -d -r name` | Detach elsewhere, reattach here |
| `screen -D -RR` | Force detach and reattach (aggressive) |
| `screen -ls` | List all sessions with PIDs |
| `screen -wipe` | Clean up dead sessions |

## Windows

### Window Basics

| Command | Description |
|---------|-------------|
| `Ctrl-a c` | Create new window |
| `Ctrl-a n` | Next window |
| `Ctrl-a p` | Previous window |
| `Ctrl-a 0-9` | Switch to window by number |
| `Ctrl-a "` | List all windows (interactive) |
| `Ctrl-a '` | Switch to window by name/number |
| `Ctrl-a A` | Rename current window |
| `Ctrl-a k` | Kill current window |

### Window Management

| Command | Description |
|---------|-------------|
| `Ctrl-a w` | Show window list in status line |
| `Ctrl-a Ctrl-a` | Toggle between last two windows |
| `Ctrl-a N` | Show window number and name |
| `exit / Ctrl-d` | Close window (exit shell) |

## Split & Regions

### Split Commands

| Command | Description |
|---------|-------------|
| `Ctrl-a S` | Split horizontally (top/bottom) |
| `Ctrl-a \|` | Split vertically (left/right) |
| `Ctrl-a Tab` | Move focus to next region |
| `Ctrl-a X` | Close current region |
| `Ctrl-a Q` | Close all regions except current |

### Region Workflow

```
Ctrl-a S             # split horizontal
Ctrl-a Tab           # move to new region
Ctrl-a c             # create window in region
Ctrl-a :resize 20   # set region to 20 lines
```

## Copy Mode

### Scrollback & Copy

| Command | Description |
|---------|-------------|
| `Ctrl-a [` | Enter copy/scrollback mode |
| `Space` | Start/end selection (in copy mode) |
| `Enter` | Copy selection and exit copy mode |
| `Ctrl-a ]` | Paste copied text |
| `h/j/k/l` | Move cursor (vi keys in copy mode) |
| `Ctrl-u / Ctrl-d` | Scroll up/down half page |
| `/ or ?` | Search forward/backward |
| `Esc` | Exit copy mode |

### Copy Workflow

```
Ctrl-a [         # enter copy mode
Space            # mark start (navigate first)
Space            # mark end + copy (navigate first)
Ctrl-a ]         # paste into current window
```

## Detach & Reattach

### Detach

| Command | Description |
|---------|-------------|
| `Ctrl-a d` | Detach current session |
| `Ctrl-a D D` | Detach and log out |
| `Ctrl-a z` | Suspend screen (like Ctrl-Z) |
| `screen -d PID` | Remotely detach session by PID |

### Reattach

```
screen -r                 # reattach (if only one session)
screen -r work            # reattach by name
screen -r 12345           # reattach by PID
screen -d -r work         # steal session from other terminal
```

### Persistent Workflows

```
# Start long-running task, detach, reconnect later
screen -S build
make -j4 all        # start build
Ctrl-a d             # detach, go home
screen -r build      # reattach later
```

## Sharing

### Multi-User Mode

```
# Host enables multiuser
Ctrl-a :multiuser on
Ctrl-a :acladd alice    # grant access to user alice
# Alice attaches
screen -x hostuser/sessionname
```

### Access Control

| Command | Description |
|---------|-------------|
| `:multiuser on` | Enable multi-user mode |
| `:acladd user` | Grant user full access |
| `:aclchg user -w '#'` | Make user read-only (all windows) |
| `:acldel user` | Remove user access |
| `screen -x user/sess` | Attach to shared session |

## Configuration

### ~/.screenrc Essentials

```
startup_message off          # skip splash screen
defscrollback 10000          # scrollback buffer lines
hardstatus alwayslastline
hardstatus string '%{gk}%H %{wk}%?%-Lw%?%{bw}%n*%f %t%{wk}%?%+Lw%?'
shell -$SHELL                # use login shell
```

### Useful Settings

| Command | Description |
|---------|-------------|
| `startup_message off` | Suppress startup message |
| `defscrollback N` | Set scrollback buffer (default 100) |
| `escape ^Bb` | Change prefix key to Ctrl-b |
| `shelltitle '$ \|bash'` | Auto-set window titles |
| `caption always '%n %t'` | Always show caption with window info |
| `autodetach on` | Auto-detach on hangup (default on) |
| `vbell on` | Use visual bell instead of audible |

## Common Patterns

### Development Setup

```
# Auto-create windows in .screenrc
screen -t editor 0 vim
screen -t server 1 python -m http.server
screen -t shell  2 bash
select 0                     # start on editor window
```

### Logging & Monitoring

```
Ctrl-a H                     # toggle logging to screenlog.N
Ctrl-a M                     # monitor window for activity
Ctrl-a _                     # monitor window for silence
```

### Quick Reference

| Command | Description |
|---------|-------------|
| `Ctrl-a ?` | Show all key bindings |
| `Ctrl-a :help` | Built-in help |
| `Ctrl-a :source file` | Load config file |
| `Ctrl-a :screen cmd` | Run command in new window |
| `Ctrl-a :quit` | Kill all windows and exit session |
