tmux Cheat Sheet

In progress Linux

tmux keeps terminal sessions alive on a machine after you disconnect, and splits one terminal into windows and panes. Every shortcut below starts with the prefix, Ctrl+b by default: press it, release, then press the key.

Overview

Layer Contains Survives
Session windows detaching and SSH disconnects, until killed or the machine restarts
Window panes until its last pane closes
Pane one shell process until that shell exits; other panes keep running

Install

sudo apt update && sudo apt install -y tmux   # Debian / Ubuntu
brew install tmux                             # macOS

Sessions

Command or keys Does
tmux new -s work start a session named work (tmux alone numbers it)
prefix d or tmux detach leave the session running in the background
tmux ls list sessions
tmux attach / tmux attach -t work reattach to the last / a named session
prefix s pick a session from a list
prefix ( / prefix ) previous / next session
prefix $ rename the session
tmux kill-session -t work end a session and everything in it

Windows

Keys Does
prefix c new window
prefix n / prefix p next / previous window
prefix 0…9 go to window by number
prefix , rename the window
prefix & close the window (asks to confirm)

Panes

Keys Does
prefix " split into top and bottom panes
prefix % split into left and right panes
prefix + arrow key move to the pane in that direction
prefix z zoom the pane to full window; again to restore
prefix x close the pane (asks to confirm)
Ctrl+d (or exit) end the pane's shell, which closes the pane without asking

Command Prompt and Configuration

  • prefix : opens the command prompt, with tab completion. Every shortcut has a command behind it (split-window -h, kill-session…).
  • prefix ? lists every key binding.
  • Custom settings go in ~/.tmux.conf. Reload it without restarting: tmux source-file ~/.tmux.conf (or source-file ~/.tmux.conf at the prompt).

Script a Development Session

A shell script can build the whole layout, then attach to it:

SESSION=dev
tmux kill-session -t "$SESSION" 2>/dev/null          # start clean
lsof -ti:8000,8001 | xargs kill 2>/dev/null          # free the dev servers' ports
tmux new-session -d -s "$SESSION" -n main             # -d: create without attaching
tmux new-window -t "$SESSION" -n editor 'nvim .'
tmux new-window -t "$SESSION" -n api
tmux send-keys -t "$SESSION:api" 'make fastapi-dev' C-m
tmux select-window -t "$SESSION:editor"
tmux attach -t "$SESSION"

send-keys … C-m types the command and presses Enter, so the window keeps its shell when the server stops, unlike a command passed to new-window, whose window closes with it. A plain kill lets the old servers exit cleanly; keep kill -9 for processes that ignore it. Wrapping the script in a make dev target and the teardown (tmux kill-session) in make stop gives the whole team the same environment.

References

Review cards

Flashcard What happens when you use `prefix d` or `tmux detach`?

It detaches the session from the terminal but leaves all processes running.

Flashcard How can you rename a tmux session?

Use prefix $ to rename the session.

Quiz Which of the following commands would you use to start a new tmux session named 'work'?
  • tmux new -s work
  • tmux -s work
  • tmux new work
  • tmux -n work

The correct command to start a new tmux session named 'work' is tmux new -s work.

Flashcard How can you kill a tmux session and everything in it?

Use tmux kill-session -t work to end a session and all its panes and windows.

Quiz Which tmux keys split the current pane into top and bottom panes?
  • prefix "
  • prefix %
  • prefix ;
  • prefix ,

prefix " splits top and bottom; prefix % splits left and right.