Note to self: how to use TMUX
This is just a simple cheatsheet on how to use TMUX (terminal multiplexer) in the following use-case:
you plan to start a long running job in a remote shell and you want to be able to exit the session without losing the job execution; or more commonly, you are in the middle of a job execution and your local network drops, which makes your session do the dodo.
Disclamer: This is not a tutorial on TMUX. There are plenty of resources out there, including the book tmux. You can find this book as a PDF on the web, but please consider purchasing the book to support its author and future development - there’s also a new version, tmux 2.
In your remote shell, begin a new TMUX session:
tmux new -s <SESSION_NAME>
To detach from the session:
ctrl-b
followed by d
To list active sessions:
tmux ls
To attach to an active session:
tmux attach -t <SESSION_NAME>
To close a session while attached:
ctrl-d
or exit
To close a session while detached:
tmux kill-session -t <SESSION_NAME>
Added (20 May 2020) bonus: TMUX panes are really awesome and can simplify tuning applications, while concurrently monitoring log files. This is especially helpful when operating on remote servers. The following list of TMUX "pane" commands were lifted from the tmuxcheatsheet.com website:
Shortcut | Action |
---|---|
Ctrl-b + % | split-window -h (split horizontally) |
Ctrl-b + " | split-window -v (split vertically) |
Ctrl-b + { | Move the current pane left |
Ctrl-b + } | Move the current pane right |
Ctrl-b + (↑ ↓ ← →) | Switch to pane to the direction |
Ctrl-b + q | Show pane numbers |
Ctrl-b + 0 … 9 | Switch/select pane by number |
Ctrl-b + z | Toggle pane zoom |
Ctrl-b + ! | Convert pane into a window |
Ctrl-b Ctrl + (↑ ↓) | Resize current pane height (after Prefix, hold down the Ctrl key while tapping the up or down key) |
Ctrl-b Ctrl + (← →) | Resize current pane width (after Prefix, hold down the Ctrl key while tapping the right or left key) |
Ctrl-b + x | Close current pane |
Ctrl-b + Spacebar | Toggle between pane layouts |
Ctrl-b + o | Switch to next pane |
Tip (30 March 2022): I added a user .tmux.conf
file in my home directory and entered a control for toggling window synchronization so that commands entered into one pane will be synchronized to all panes. This allows me to perform monotonous editing commands in the same file or file-system operations in multiple servers at once. The control is:
bind C-s set-window-option synchronize-panes
and is toggled by the sequence Ctrl-b Ctrl-s
. You can also start "tmux" on the command line and have it create a number of panes. For example, to start three horizontal panes:
tmux new-session \; \split-window -v \; \split-window -v