# tmux — Manage Multiple Terminal Sessions in One Window

> Practical guide to tmux: start sessions, split windows and panes, detach and reattach – terminal sessions survive SSH drops and logout.

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

<!-- PROSE:intro -->
tmux is a terminal multiplexer: it bundles multiple terminal sessions, windows and panes into a single window – and keeps them alive even after you disconnect. That is exactly what makes it indispensable on remote servers: an SSH connection may drop or you may log out, yet your running processes keep working undisturbed in the detached session. This guide walks you through the commands and key bindings you actually reach for daily, from creating named sessions to your own `~/.tmux.conf`.
<!-- PROSE:intro:end -->

## Sessions

`tmux` — Start a new unnamed session.

```bash
tmux
```

`tmux new -s <name>` — Start a new named session.

```bash
tmux new -s project
```

`tmux ls` — List all active sessions.

```bash
tmux ls
```

`tmux attach -t <name>` — Attach to an existing session by name or number.

```bash
tmux attach -t project
```

`tmux attach` — Attach to the most recently used session.

```bash
tmux attach
```

`tmux kill-session -t <name>` — Kill a specific session.

```bash
tmux kill-session -t project
```

`tmux kill-server` — Kill the tmux server and all sessions.

```bash
tmux kill-server
```

`tmux rename-session -t <old> <new>` — Rename an existing session.

```bash
tmux rename-session -t 0 main
```

`tmux switch -t <name>` — Switch to a different session from within tmux.

```bash
tmux switch -t project
```

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

`Ctrl+b d` — Detach from the current session (session keeps running in background).

```bash
Ctrl+b d
```

`Ctrl+b $` — Rename the current session.

```bash
Ctrl+b $
```

`Ctrl+b s` — Show session list and switch interactively.

```bash
Ctrl+b s
```

`Ctrl+b (` — Switch to the previous session.

```bash
Ctrl+b (
```

`Ctrl+b )` — Switch to the next session.

```bash
Ctrl+b )
```

`Ctrl+b L` — Switch to the last (most recently used) session.

```bash
Ctrl+b L
```

## Windows (Tabs)

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

```bash
Ctrl+b c
```

`Ctrl+b ,` — Rename the current window.

```bash
Ctrl+b ,
```

`Ctrl+b w` — List all windows and select interactively.

```bash
Ctrl+b w
```

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

```bash
Ctrl+b n
```

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

```bash
Ctrl+b p
```

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

```bash
Ctrl+b 2
```

`Ctrl+b &` — Close the current window (with confirmation).

```bash
Ctrl+b &
```

`Ctrl+b l` — Toggle to the last active window.

```bash
Ctrl+b l
```

`tmux swap-window -s <src> -t <dst>` — Swap two windows by their index numbers.

```bash
tmux swap-window -s 2 -t 0
```

`tmux move-window -t <index>` — Move the current window to a new index.

```bash
tmux move-window -t 5
```

## Panes (Splits)

`Ctrl+b %` — Split the current pane vertically (left/right).

```bash
Ctrl+b %
```

`Ctrl+b "` — Split the current pane horizontally (top/bottom).

```bash
Ctrl+b "
```

`Ctrl+b <arrow>` — Move focus to the pane in the arrow direction.

```bash
Ctrl+b ←
```

`Ctrl+b o` — Cycle through panes in the current window.

```bash
Ctrl+b o
```

`Ctrl+b q` — Show pane numbers briefly. Press a number to jump to that pane.

```bash
Ctrl+b q
```

`Ctrl+b x` — Close the current pane (with confirmation).

```bash
Ctrl+b x
```

`Ctrl+b z` — Toggle zoom on the current pane (fullscreen/restore).

```bash
Ctrl+b z
```

`Ctrl+b {` — Swap the current pane with the previous one.

```bash
Ctrl+b {
```

`Ctrl+b }` — Swap the current pane with the next one.

```bash
Ctrl+b }
```

`Ctrl+b Space` — Cycle through preset pane layouts (even-horizontal, even-vertical, etc.).

```bash
Ctrl+b Space
```

## Pane Resizing

`Ctrl+b Ctrl+<arrow>` — Resize the current pane by 1 cell in the arrow direction.

```bash
Ctrl+b Ctrl+→
```

`Ctrl+b Alt+<arrow>` — Resize the current pane by 5 cells in the arrow direction.

```bash
Ctrl+b Alt+→
```

`tmux resize-pane -D <n>` — Resize the current pane down by n rows.

```bash
tmux resize-pane -D 10
```

`tmux resize-pane -U <n>` — Resize the current pane up by n rows.

```bash
tmux resize-pane -U 5
```

`tmux resize-pane -L <n>` — Resize the current pane left by n columns.

```bash
tmux resize-pane -L 10
```

`tmux resize-pane -R <n>` — Resize the current pane right by n columns.

```bash
tmux resize-pane -R 10
```

## Copy Mode & Scrollback

`Ctrl+b [` — Enter copy mode to scroll and select text. Use arrow keys or Page Up/Down.

```bash
Ctrl+b [
```

`q` — Exit copy mode (while in copy mode).

```bash
q
```

`Space` — Start selection (while in copy mode with vi keys).

```bash
Space
```

`Enter` — Copy selection and exit copy mode (with vi keys).

```bash
Enter
```

`Ctrl+b ]` — Paste the most recently copied text.

```bash
Ctrl+b ]
```

`tmux capture-pane -p` — Capture the visible pane contents and print to stdout.

```bash
tmux capture-pane -p > output.txt
```

`tmux capture-pane -p -S -<n>` — Capture n lines of scrollback history.

```bash
tmux capture-pane -p -S -1000 > scrollback.txt
```

## Command Mode & Miscellaneous

`Ctrl+b :` — Enter tmux command prompt (type any tmux command without the 'tmux' prefix).

```bash
Ctrl+b : split-window -h
```

`Ctrl+b ?` — Show all key bindings.

```bash
Ctrl+b ?
```

`Ctrl+b t` — Show a large clock in the current pane.

```bash
Ctrl+b t
```

`tmux source-file ~/.tmux.conf` — Reload the tmux configuration file.

```bash
tmux source-file ~/.tmux.conf
```

`tmux info` — Show tmux server information and terminal capabilities.

```bash
tmux info
```

`tmux list-keys` — List all key bindings and their commands.

```bash
tmux list-keys
```

## Configuration (~/.tmux.conf)

`set -g mouse on` — Enable mouse support for scrolling, pane selection, and resizing.

```bash
set -g mouse on
```

`set -g prefix C-a` — Change the prefix key from Ctrl+b to Ctrl+a.

```bash
set -g prefix C-a
unbind C-b
bind C-a send-prefix
```

`set -g base-index 1` — Start window numbering from 1 instead of 0.

```bash
set -g base-index 1
setw -g pane-base-index 1
```

`set -g history-limit <n>` — Set the scrollback buffer size (number of lines).

```bash
set -g history-limit 50000
```

`setw -g mode-keys vi` — Use vi-style key bindings in copy mode.

```bash
setw -g mode-keys vi
```

`set -g default-terminal "screen-256color"` — Set the default terminal type for 256-color support.

```bash
set -g default-terminal "screen-256color"
```

`bind | split-window -h` — Bind a custom key for splitting panes (e.g., | for vertical, - for horizontal).

```bash
bind | split-window -h
bind - split-window -v
```

<!-- PROSE:outro -->
## Conclusion

tmux turns a single terminal into a full-blown workspace: `tmux new -s <name>` starts a named session, `Ctrl+b d` detaches you, and `tmux attach -t <name>` drops you back in exactly where you left off – even after an SSH drop or a logout. This persistence is what sets it apart from a plain terminal and makes it the modern, more powerful alternative to `screen`. For one-off background jobs without an interactive interface, `nohup` is often enough – but as soon as you need multiple windows, panes and a detachable session, tmux is the tool of choice. Prefix, mouse support and custom key bindings are configured permanently in `~/.tmux.conf`.

## Further Reading

- [tmux Wiki on GitHub](https://github.com/tmux/tmux/wiki) – the official wiki with a getting-started guide, key bindings and configuration
- [Arch Wiki: tmux](https://wiki.archlinux.org/title/Tmux) – detailed reference for configuration and practical tips
<!-- PROSE:outro:end -->

## Related Commands

- [screen](https://www.jpkc.com/db/en/cheatsheets/shell-system/screen/) – the classic terminal multiplexer, predecessor and alternative to tmux
- [nohup](https://www.jpkc.com/db/en/cheatsheets/shell-system/nohup/) – detach processes from the terminal so they survive logout and SSH drops
- [bash](https://www.jpkc.com/db/en/cheatsheets/shell-system/bash/) – the shell that runs inside every tmux pane

