Basics of tmux
& cheat sheet
tmux
use cases:
- Locally, as a window manager for your terminal
- On a remote server over SSH1, as a way to persist terminal sessions between logins and protect against network interruptions
tmux
is text-based, cross-platform, works with any terminal emulator, and can be scripted.
Getting started
- Install
tmux
via your system’s package manager. - Run
tmux
in a terminal. (To exit:Ctrl + d
orexit
.) - Now try the shortcuts in the next section. E.g. to split the window in two:
Ctrl + b
, then lift your fingers off the keyboard for a moment, then type a percent symbol (Shift + 5
).
Essentials shortcuts
All you need to be productive with tmux
:
Notation:
C-b
meansCtrl + b
.M-b
meansAlt + b
(theM
stands for modifier key).C-b c
(separated by a space) means first pressCtrl + b
, then lift your fingers off the keyboard, then pressc
.
In summary: separated by a hyphen means press together, separated by a space means release keys before continuing.
Shortcut | Action |
---|---|
C-b ? | List all shortcuts with explanations |
C-b c | Create new tab |
C-b , | Rename tab (Enter to finalize) |
C-b l | Switch to previous active tab |
C-b 3 | Switch to tab with index 3 |
C-b " | Split window vertically |
C-b % | Split window horizontally |
C-b z | Maximize/un-maximize current pane horizontally |
C-b <arrow key> | Switch to the pane to the <arrow key> direction of the current active pane |
C-b M-<arrow key> | Resize current pane in the <arrow key> direction |
C-b d | Detach session |
C-b [ | Enter copy2 mode (enables you to scroll via arrow keys or mouse wheel) |
q | Exit copy mode |
C-<space> | Begin copying (use arrow keys to select text) |
M-w | Copy text to tmux ’s buffer |
C-b ] | Paste text from tmux ’s buffer |
Commands
tmux
to start.
Sample output of tmux list-sessions
:
0: 3 windows (created Thu Nov 4 04:11:30 2021)
The first number in the above output (0
) is the target you’d use to attach the session:
tmux attach-session -t 0
(Detach session via shortcut C-b d
.)
Remote sessions3
Problem:
- You have a remote server that you access via SSH.
- You want to run an app on that server via command
node server.js
. - How do you close the SSH connection without shutting down the app?
Solution:
- SSH into the remote server.
- Start a
tmux
session on the remote server. - Start your app via command
node server.js
. - Detach the
tmux
session via shortcutC-b d
. - Close the SSH connection.
- Later, SSH back into the remote server.
- Run
tmux list-sessions
to see that the session from step 2 is still running and get its session ID (probably zero). - Run
tmux attach-session -t 0
to attach the session (assuming the session ID is zero). Now you’re back where you were after step 3.
Caveat:
What if you’re running tmux
locally? You’re accessing a remote session from your local session. tmux
inside tmux
. The caveat: by default, the local session will intercept all shortcuts you issue—how do you issue shortcuts to the nested remote session? Simple: just issue the C-b
prefix twice. For example, to detach the remote session: C-b C-b d
.
See also: my blog post on how to use automate tmux
Footnotes
-
Drop-in replacement for SSH: Mosh. Simple as
mosh user@host
instead ofssh user@host
(after installing Mosh locally and on remote server). ↩ -
See also traditional command line utils for copy/pasting, e.g.
pbcopy
andpbpaste
on macOS andxclip
andxsel
on Linux. ↩ -
This is an alternative to foreground/background jobs. It is often recommended to use
systemd
to manage production server processes. ↩