Useful site: https://tmuxcheatsheet.com/ (credit: most of what you find here is ripped from that -- one day I'll read the actual tmux docs.) Also see the TmuxMultiplexer wiki at: https://github.com/tmux/tmux/wiki

Use Ctrl-B to control Tmux. Use Ctrl-B : to get into command mode.

Cheat Sheet

Command line

You can do Ctrl-B : command or from the command line, e.g.

$ tmux new-window

Keys

Key combos involve pressing Ctrl-B followed by something.

Commands

Ctrl-B : enters command mode. In command mode there is tab-completion.

Use Ctrl-B followed by

: attach -d -- detach all other clients

Session

Keybindings: Ctrl-B followed by

$ -- rename session
d -- detach from session
( -- previous session
) -- next session
s -- list sessions
and commands
:ls, :list-sessions              -- list sessions
:new                             -- new session
:new -s session_name             -- new session with name session_name
:kill-ses                        -- short for :kill-session
:kill-session -t session_name    -- kill session with name session_name
:kill-session -a -t session_name -- kill all sessions but session_name
:attach-session -t session_name  -- attach to session with name session_name
:a -t, :at -t, :attach -t        -- shorthands for :attach-session 

Window

Keybindings: Ctrl-B followed by

c -- create window
, -- rename window
& -- close window
n -- next window
p -- previous window
0..9 -- change to window number 0..9
and commands
:swap-window -t -1     -- move current window left by one position
:swap-window -t n      -- swap current window with window at position n
:swap-window -t n -s m -- swap window n with window m

Windows to/from Panes

Commands

:move-pane -t <window number>:<split number>
:join-pane -s <src window number> -t <target window number> # one above t'other
:join-pane -s <src> -t <tgt> -h # side by side
:break-pane # break out current pane as separate window

Panes

Ctrl-B followed by

; -- toggle last active pane
% -- split vertically (side by side)
" -- split horizontally (one above the other)
{ -- move current pane left
} -- move current pane right
arrow -- move to pane in direction of arrow (e.g. left arrow switches to pane to left)
q -- show pane numbers (and sizes)
q 0..9 -- switch to numbered pane (you have about a second to press the number)
z -- toggle pane zoom (make pane fill entire window / revert )
! -- convert pane to window
x -- close pane
Ctrl-arrow -- resize pane (one row/col at a time)

Alternative for resizing: hold Ctrl-B and press

arrow -- to resize pane (e.g. with right pane selected, Ctrl-b-leftarrow enlarges right pane by moving divider to the left) this is preferable

Commands

:setw synchronize-panes  -- toggle synchronize panes (keys to to all panes)

Referring to windows and panes

For panes, use e.g. %21. For windows, use e.g. @42. To see identifiers, use

list-panes
list-windows
and then e.g.
:join-pane -s %21 -t @42

Copy mode

Keybindings: Ctrl-B followed by

[     -- enter copy mode
Pgup  -- enter copy mode and scroll one page up
q     -- quit copy mode
g     -- go to top line
G     -- go to bottom line
Up    -- scroll up
Down  -- scroll down
h     -- move cursor left
j     -- move cursor down
k     -- move cursor up
l     -- move cursor right
w     -- move cursor forward one word
b     -- move cursor back one word
/     -- search forward
?     -- search backward
n     -- next keyword occurrence
N     -- previous keyword occurrence
Space -- start selection
Esc   -- clear selection
Enter -- copy selection
]     -- paste contents of buffer_0

Commands:

:setw -g mode-keys vi  -- use vi keys in buffer
:capture-pane          -- copy entire visible contents of pane to buffer_0
:list-buffers          -- show all buffers
:choose-buffer         -- show all buffers and paste selected
:save-buffer buf.txt   -- save buffer contents to buf.txt
:delete-buffer -b 1    -- delete buffer_1
(similarly we can use -b n with most buffer commands)

Shorthand scripts

I use these two scripts, which I call tm and tmx respectively

#!/bin/bash

# usage: tm <session> # to start new session named <session>
#        tm           # as shorthand for tmux
if [ -z "$1" ]; then
  tmux
else
  S="$1"
  shift
  tmux new -s "$S" "$@"
fi
#!/bin/bash

# usage: tmx <session> # to attach to session,
#        tmx           # to list sessions
# like screen -x -r "$@"

if [ -z "$1" ]; then
  tmux ls
else
  S="$1"
  shift
  tmux attach -t "$S" "$@"
fi

Launching scripts in new sessions

tmux new-session -s "session-name" "command"
tmux new-session -d -s "session-name" "command" # start detached
or use the shorter
tmux new -s "session-name" "command"

Current Working Directory

Within tmux

Ctrl-B : attach -c /path/to
When starting
tmux new -s session_name -c path/to/start/in
When attaching, this will change the default directory for new windows
tmux attach -s session_name -c path/to/start/in

Config

This goes in .tmux.conf.

set-option -g display-time 4000

Wrapper Scripts

I usually define the two following scripts. First tm for launching a new session:

#!/bin/bash

if [ -z "$1" ]; then
  tmux
else
  S="$1"
  shift
  tmux new -s "$S" "$@"
fi
then tmx for listing sessions (if no arg) and attaching to a running session (if an arg is given)
#!/bin/bash

if [ -z "$1" ]; then
  tmux ls
else
  S="$1"
  shift
  tmux attach -t "$S" "$@"
fi

Attaching and Detaching

Detaching other sessions

Ctrl-B D
}}}