tmux Quick Reference
Keep sessions alive across disconnects and split your terminal like a pro. Everything is prefix (Ctrl-b) then a key.
tmux is a terminal multiplexer: a server process owns your shells, and the terminal window you look at is just a client attached to it. Close the terminal or drop the SSH connection and the server keeps everything running, so you can reattach later from anywhere.
It nests sessions (one per project) holding windows (tabs) split into panes. Every shortcut is the prefix — Ctrl-b by default — released, then one key: prefix d means press Ctrl-b, let go, press d. prefix ? lists every binding.
Sessions (survive disconnect)
- New named session
tmux new -s work- Detach
prefix d— session keeps running- List / attach
tmux ls·tmux attach -t work- Kill session
tmux kill-session -t work- Rename
prefix $- tmux new -A -s work
- attach if
workexists, create it if not — one command for every login - tmux new -d -s build 'make all'
- start a detached session running a command; its window closes when the command exits
- tmux attach -d -t work
- attach and detach every other client, such as a forgotten one on another machine
- prefix s
- interactive tree of sessions and windows — arrows to pick, Enter to switch
- prefix ( / prefix )
- previous / next session;
prefix Ljumps back to the last one - tmux kill-server
- end every session at once, and every program running inside them
Windows (tabs)
- New / next / prev
prefix c·prefix n·prefix p- Jump to number
prefix 0..9- Rename window
prefix ,- List windows
prefix w- prefix l
- toggle to the last window you were in
- prefix &
- kill the window and every pane in it (asks to confirm)
- prefix .
- move the window to another index — renumber your tabs
- prefix f
- find a window by text shown in any of its panes
- tmux new-window -n logs
- scriptable new window with a name;
-c dirsets its working directory
Panes (split)
- Split vertical / horizontal
prefix %(left/right) ·prefix "(top/bottom)- Move between panes
prefixthen arrow keys · orprefix o- Zoom a pane
prefix z— toggle fullscreen- Close pane
prefix x· or justexit- Cycle layouts
prefix Space- prefix q
- flash pane numbers — press a number while they show to jump there
- prefix ;
- back to the previously active pane
- prefix { / prefix }
- swap the current pane with the previous / next one
- prefix !
- break the pane out into a window of its own
- prefix Ctrl-arrow
- resize the pane by one cell;
prefix Alt-arrowresizes by five - prefix Alt-1 … Alt-5
- preset layouts: even horizontal, even vertical, main horizontal, main vertical, tiled
- setw synchronize-panes on
- type into every pane of the window at once — same command on many servers
Copy mode
- Enter scroll/copy
prefix [— then move;qto exit- prefix PgUp
- enter copy mode and scroll up a page in one step
- Space … Enter
- vi keys: start a selection, then copy it and leave copy mode
- Ctrl-Space … Alt-w
- the same in emacs keys, which are the default
- / and ?
- search forward / backward in vi mode (
nrepeats); emacs mode usesCtrl-s/Ctrl-r - prefix ]
- paste the most recent copy;
prefix =picks from older buffers - mode-keys default
- emacs, unless
$EDITORor$VISUALcontains “vi” — then vi
Config (~/.tmux.conf)
- Reload config
prefix :thensource-file ~/.tmux.conf- Common tweak
set -g mouse onfor click-to-select panes, drag-to-resize and scrolling- set -g prefix C-a
- move the prefix to Ctrl-a; also
unbind C-bandbind C-a send-prefix - set -g base-index 1
- number windows from 1, matching the keyboard;
setw -g pane-base-index 1for panes - set -g history-limit 50000
- scrollback lines per pane (default 2000); applies to panes created afterwards
- setw -g mode-keys vi
- vi keys in copy mode, whatever
$EDITORsays - set -sg escape-time 10
- shorter wait after Esc — stops the lag vim users notice
- set -g renumber-windows on
- close window 2 of 4 and the rest renumber with no gap
- set -g default-terminal "tmux-256color"
- full color and italics; use
screen-256colorwhere that terminfo entry is missing
Scripting & misc
- tmux send-keys -t work:1 'make' Enter
- type into a pane from a script;
Enterpresses Return - tmux capture-pane -p -S -1000
- print the last 1,000 lines of a pane’s scrollback to stdout
- tmux pipe-pane -o 'cat >> pane.log'
- log everything a pane prints from now on; run it again to stop
- tmux display -p '#S:#I.#P'
- print the current session:window.pane — handy in scripts and prompts
- prefix prefix
- send the prefix through to a tmux nested inside (e.g. over SSH)
- prefix :
- command prompt: any tmux command, tab-completed
- prefix ~
- recent tmux messages — where config errors show up
Worked example: run the same command on four servers at once
You need to check disk space and restart a service on four web servers. Logging into each in turn is slow and easy to get wrong. Open one pane per server and type once:
tmux new -d -s fleet 'ssh web1' # detached session, first pane is ssh web1
for h in web2 web3 web4; do
tmux split-window -t fleet "ssh $h" # one more pane per server
tmux select-layout -t fleet tiled # rebalance, so the next split has room
done
tmux display -p -t fleet '#{window_panes}'
4
tmux setw -t fleet synchronize-panes on
tmux attach -t fleet
- Each pane runs
sshdirectly, not a shell, so when a connection ends its pane closes on its own. - Rebalancing after every split matters. A detached session starts small (80×24 by default), and halving the newest pane each time would leave the last two panes about two rows tall.
- With
synchronize-paneson, typingdf -h /and Enter sends the same keys to all four panes, and you read four answers side by side. - To act on one server only, turn it off with
prefix :thensetw synchronize-panes off, pick the pane withprefix q, fix it, and turn sync back on.
Be careful with anything destructive. Synchronised keys land in every pane, including one where the previous command is still running or a prompt is waiting.
Recipes
Keep a long job running after you disconnect
ssh server
tmux new -A -s deploy # attach if it exists, create it if not
./long-migration.sh # start the work
# press prefix d to detach, then log out: the job keeps running
ssh server
tmux attach -t deploy # back exactly where you left itThe session lives as long as the tmux server process — a reboot still ends it. new -A makes the same command safe to run on every login.
A config that fixes the defaults
# ~/.tmux.conf
set -g prefix C-a # easier to reach than C-b
unbind C-b
bind C-a send-prefix # press C-a twice to send it to the program
set -g mouse on # click panes, drag borders, scroll
set -g base-index 1 # windows start at 1, like the keyboard
setw -g pane-base-index 1
set -g renumber-windows on # no gaps after closing a window
set -g history-limit 50000 # scrollback per pane
setw -g mode-keys vi # vi keys in copy mode
set -sg escape-time 10 # no lag after Esc in vim
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind r source-file ~/.tmux.conf \; display "config reloaded"The two bind lines split into the current pane’s directory instead of the session’s start directory. Reload with prefix r; options like history-limit only affect panes created afterwards.
Copy text from the scrollback (vi keys)
prefix [ # enter copy mode
?error # search backward for "error"; n jumps to the next match
Space # start the selection at the cursor
j j $ # extend it with vi motions
Enter # copy the selection and leave copy mode
prefix ] # paste it into the current paneWith the mouse on, dragging selects and copies too. Hold Shift (Option in iTerm2) to bypass tmux and use the terminal’s own selection — usually the quickest route to the system clipboard.
Script a development layout
tmux new -d -s dev -n editor # detached session, first window "editor"
tmux send-keys -t dev:editor 'vim .' Enter
tmux new-window -t dev -n server
tmux send-keys -t dev:server 'npm run dev' Enter
tmux split-window -h -t dev:server # new pane beside the server
tmux send-keys -t dev:server 'npm test -- --watch' Enter
tmux attach -t devTargets read session:window.pane, and names work in place of numbers. Without a .pane part, keys go to the window’s active pane — right after split-window, that is the new one.
Save a pane’s output to a file
tmux capture-pane -p -J -S -10000 > pane.txt # last 10,000 lines, wrapped lines joined
tmux pipe-pane -o 'cat >> ~/session.log' # log everything from now on; repeat to stopcapture-pane can only return what is still in scrollback, so raise history-limit before you need it. -S - starts from the very beginning of the history.
When to use it, and when not
- Long jobs over SSH. A migration, a big copy or a training run should not die when your Wi-Fi does.
- Persistent workspaces. One session per project, with editor, server and logs already open when you come back.
- Pairing. Two people attached to the same session see and type into the same panes.
Pick something else when it fits better. For local splits only, your terminal emulator’s own panes are simpler. For one fire-and-forget job, nohup cmd > job.log 2>&1 & is enough. For anything that must restart after a crash or a reboot, write a systemd service. Plugins such as tmux-resurrect can restore layouts after a reboot, but not the state of the programs that were running.
Trade-offs and limits
| tmux | nohup … & | systemd service | |
|---|---|---|---|
| Survives an SSH drop | yes | yes | yes |
| Survives a reboot | no | no | yes, if enabled |
| Interact with it later | yes, reattach | no, read the log | no, read the journal |
| Restarts after a crash | no | no | yes, with Restart= |
tmux has everyday costs too. The prefix steals a key from every program inside it, nested tmux over SSH needs the prefix twice, and text you copy lands in a tmux buffer rather than the system clipboard unless your terminal supports it (set -g set-clipboard on uses the OSC 52 escape sequence where the terminal allows it).
Common mistakes
- Starting tmux inside tmux by accident. tmux refuses unless you unset
$TMUX, and forcing it means every key needs the prefix twice. Checkecho $TMUX: if it is set, you are already inside. - Typing
exitto leave. In the last pane that ends the shell, the window and the session with it.prefix dis the one that leaves everything running. - Editing
~/.tmux.confand expecting new sessions to pick it up. The config is read when the server starts. While any session is alive, new ones join the same server, so runtmux source-file ~/.tmux.confinstead. - Forgetting
Enterinsend-keys.tmux send-keys -t dev 'make'only types the word. AddEnteras a separate argument to run it. - Trusting tmux with a reboot. The server is an ordinary process. A reboot or
tmux kill-serverends every program inside, saved or not.
Check yourself
Session work already exists. What does tmux new -A -s work do?
It attaches to the existing session. -A makes new-session behave like attach-session when the name is taken, so the same command is safe to run on every login.
What does the target -t dev:2.1 point at?
Pane 1 of window 2 in session dev. Targets read session:window.pane, and the numbers follow base-index and pane-base-index.
You change ~/.tmux.conf, run tmux new -s two while session one is open, and nothing changes. Why?
Both sessions live in the same server, which read the config once when it started. Run tmux source-file ~/.tmux.conf, or bind it to a key, to apply the edit.
Your prefix is C-a with bind C-a send-prefix. How do you jump to the start of the line in bash?
Press Ctrl-a twice. The first press is taken as the prefix and the second, bound to send-prefix, sends a real Ctrl-a to the shell.
Questions about tmux Cheat Sheet
How do I scroll up in tmux?
tmux draws the screen itself, so your terminal’s own scrollbar only sees the current view. Press the prefix and then the left bracket key to enter copy mode, then scroll with the arrow keys, Page Up and Page Down, or Ctrl-b and Ctrl-f with vi keys; prefix then Page Up enters copy mode and scrolls in one step. Press q to return to normal. For mouse-wheel scrolling, add set -g mouse on to your tmux.conf. How far back you can go is set by history-limit, which defaults to 2000 lines per pane.
How do I detach and reattach a tmux session?
Press the prefix and then d to detach: the session and every program in it keep running in the background, even if you close the terminal or your SSH connection drops. Later, run tmux ls to see the running sessions and tmux attach -t name to reconnect, or just tmux attach for the most recent one. tmux new -A -s name attaches when the session exists and creates it otherwise, which makes a good login habit. If another client is attached and the window looks shrunk, attach with -d to detach the others.
What is the difference between tmux sessions, windows and panes?
They nest. A session is the top level, usually one per project or task, and it is what you detach from and reattach to. Each session holds one or more windows, which behave like tabs: only one is visible at a time, and the status bar lists them. Each window can be split into panes, rectangles that are all visible at once and each run their own shell. A typical setup is one session per project, a window each for the editor, the server and the logs, and panes inside a window for things you want to watch side by side.
How do I change the tmux prefix to Ctrl-a?
Add three lines to ~/.tmux.conf: set -g prefix C-a to make Ctrl-a the prefix, unbind C-b to free the old one, and bind C-a send-prefix so pressing Ctrl-a twice still sends a literal Ctrl-a to the program inside, which matters because the shell uses it to jump to the start of the line. Then reload with tmux source-file ~/.tmux.conf, or restart tmux. Ctrl-a is the habit of former GNU screen users; others prefer Ctrl-Space, because it collides with nothing in the shell.