tmux keeps your programs running on the server after your SSH connection drops. Every shortcut below starts with the prefix, Ctrl-b: press that, let go, then press the next key.
Sessions
tmux # start a new sessiontmux new -s deploy # start one with a nametmux ls # list sessionstmux attach # go back to the latest onetmux attach -t deploy # go back to this onetmux kill-session -t deploy # end onetmux kill-server # end all of themCtrl-b d detach (everything keeps running)Ctrl-b s choose a session from a listCtrl-b $ rename this sessionWindows
Windows are like tabs.
Ctrl-b c new windowCtrl-b n next windowCtrl-b p previous windowCtrl-b 0..9 go to window numberCtrl-b w choose from a listCtrl-b , rename this windowCtrl-b & close this window (asks first)Panes
Panes split a window.
Ctrl-b % split left and rightCtrl-b " split top and bottomCtrl-b arrow move to the next paneCtrl-b o next paneCtrl-b z zoom this pane (press again to go back)Ctrl-b x close this pane (asks first)Ctrl-b ! turn this pane into a windowCtrl-b Space change the layoutCtrl-b q show pane numbersCtrl-b { swap with the previous paneCtrl-b } swap with the next paneTo resize, hold Ctrl and the prefix’s b, then press an arrow key, or use :resize-pane -D 5 for down by 5.
Scroll and copy
Your mouse wheel may not work until you turn it on (below), so use copy mode:
Ctrl-b [ enter copy mode, then arrows or PgUp to scrollq leave copy modeSpace start selectingEnter copy the selectionCtrl-b ] pasteIn copy mode, press / to search forward and ? to search backward. If you use Vim keys, set them in the config (below).
Commands
Ctrl-b : type a tmux commandCtrl-b ? list every shortcutCtrl-b t show a clocktmux send-keys -t deploy 'uptime' Enter # type into a session from outsidetmux new -d -s job './backup.sh' # run something in the backgroundtmux capture-pane -pt deploy | tail -n 20 # read what's on its screenRun the same thing on every pane
Ctrl-b :setw synchronize-panes onCtrl-b :setw synchronize-panes offThat’s useful with one pane per server. Be careful, as everything you type goes to all of them.
Share a session
Two people logged in as the same user can both attach, and see the same screen:
tmux new -s pair # first persontmux attach -t pair # second personA small config
Put this in ~/.tmux.conf:
set -g mouse on # scroll and click with the mouseset -g history-limit 50000 # keep more scrollbackset -g base-index 1 # number windows from 1setw -g pane-base-index 1 # and panessetw -g mode-keys vi # Vim keys in copy modeset -g escape-time 10 # no lag after Esc in Vimbind r source-file ~/.tmux.conf \; display "Reloaded"To load it into a running session: tmux source-file ~/.tmux.conf.
Prefer Ctrl-a as the prefix, as in GNU screen? Add these lines:
unbind C-bset -g prefix C-abind C-a send-prefixWhen things look wrong
no sessions: the server rebooted, or the last session ended. tmux keeps sessions in memory, so they don’t survive a reboot.sessions should be nested with care: you’re already inside tmux, as with one on your laptop and another on the server. Press the prefix twice to reach the inner one.open terminal failed: missing or unsuitable terminal: yourTERMisn’t known on the server. RunTERM=xterm-256color tmux.- Your screen stops updating, or shows only dots: another smaller client is attached. Use
tmux attach -d, which detaches others. - SSH keys or
DISPLAYstopped working in an old session: the session kept the settings from when it was created. Start a new one, or update them by hand.
Other things you may need
- Vim Cheat Sheet
- Linux Troubleshooting Cheat Sheet
- The SSH Config Generator, to keep SSH connections alive