Guides

tmux — Terminal Multiplexer

What it is

tmux is a terminal multiplexer: one terminal window can hold many shell sessions at once, and those sessions keep running even when you disconnect.

Think of it as a window manager that lives inside your terminal:

The killer feature is the detach/reattach model. Your programs run inside the tmux server, not inside your terminal. Close the laptop, drop the VPN, get kicked off SSH — the server keeps everything running, and you pick up exactly where you left off with tmux attach.

How to use

Starting out

tmux                        # start a new unnamed session
tmux new -s work            # start a new session named "work"
tmux ls                     # list running sessions
tmux attach                 # reattach to the most recent session
tmux attach -t work         # reattach to a session by name
tmux kill-session -t work   # end a session and everything in it

The prefix key

Every tmux keyboard command starts with a prefix: Ctrl+b by default. Press and release the prefix, then press the command key. So “split the window” is: hold Ctrl, press b, release both, press %.

Written shorthand throughout this guide: prefix % means Ctrl+b then %.

A typical workflow

# on the server
tmux new -s etl             # 1. create a workspace for the task
# ... start a long-running job in one pane,
#     tail its log in another (prefix % to split) ...
# prefix d                  # 2. detach — the job keeps running
exit                        # 3. log out, go home

# later, from anywhere
ssh the-server
tmux attach -t etl          # 4. everything is still there, still running

Configuration

tmux reads ~/.tmux.conf at server start. A minimal, widely useful config:

set -g mouse on              # click to select panes, scroll to scroll
set -g history-limit 50000   # keep plenty of scrollback
set -g base-index 1          # number windows from 1 (0 is far from the keys)
set -g status-interval 5     # refresh the status bar more often

Reload it into a running server with tmux source-file ~/.tmux.conf (or prefix : then source-file ~/.tmux.conf).

This repo’s scripts/terminal-bootstrap.sh installs a managed, 8-color-safe .tmux.conf block (mouse on, 50k scrollback, styled status bar) suitable for restricted HTML5-gateway terminals. The minified terminal-bootstrap-min.sh variant deliberately leaves tmux alone.

Benefits

Tips

Shortcuts

All of these are prefix (Ctrl+b) followed by the key shown.

Sessions

Keys Action
d Detach — leave everything running
s Pick a session from a list
$ Rename the current session
( / ) Previous / next session

Windows (tabs)

Keys Action
c Create a new window
, Rename the current window
n / p Next / previous window
0–9 Jump to window by number
w Pick a window from a list
& Kill the current window (confirms first)

Panes (splits)

Keys Action
% Split left/right
" Split top/bottom
← → ↑ ↓ Move between panes
o Cycle to the next pane
z Zoom pane to full screen (toggle)
x Kill the current pane (confirms first)
{ / } Swap pane with the previous / next one
Space Cycle through preset layouts
q Show pane numbers (press a number to jump)
! Break the pane out into its own window

Copy mode & misc

Keys Action
[ Enter copy mode (scroll with arrows / PgUp)
] Paste the tmux buffer
: Command prompt (type any tmux command)
t Big clock (surprisingly useful for screenshares)
? List every key binding

The copy-mode keys depend on the mode-keys option. The default is emacs. tmux uses vi only when $VISUAL or $EDITOR contains vi.

mode-keys Start a selection Copy the selection Quit
emacs (default) Ctrl+Space Alt+w or Ctrl+w q or Escape
vi (setw -g mode-keys vi) Space Enter q

In vi mode, v toggles a rectangle selection. y has no default binding. To get Vim-style v / y, add your own bindings to copy-mode-vi.

How to save your work

Detach, don’t exit — the built-in save

The everyday “save” is simply detaching (prefix d). The session — running processes, window layout, scrollback — lives on inside the tmux server until the machine reboots or you kill the session. tmux attach -t name is the matching “load”.

Saving text out of a session

Surviving a reboot

A plain tmux session does not survive a server reboot — the tmux server is just a process. Two plugins fix that:

Both install in one line each via tpm, the tmux plugin manager.

Saving the layout as code

For layouts you rebuild often, script them instead of preserving them:

#!/usr/bin/env bash
tmux new-session -d -s etl -n job
tmux split-window -h -t etl:job          # log tail beside the job
tmux send-keys  -t etl:job.0 'cd ~/etl' Enter
tmux send-keys  -t etl:job.1 'tail -f ~/etl/run.log' Enter
tmux attach -t etl

Run the script and the workspace exists; no state to back up, nothing to lose. Tools like tmuxinator do the same thing declaratively from YAML if you prefer config over script.