Nikita Zdvijkov
Software & Electrical Engineer
Houston & Remote
Blog / Jul 6, 2022

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

  1. Install tmux via your system’s package manager.
  2. Run tmux in a terminal. (To exit: Ctrl + d or exit.)
  3. 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 means Ctrl + b.
  • M-b means Alt + b (the M stands for modifier key).
  • C-b c (separated by a space) means first press Ctrl + b, then lift your fingers off the keyboard, then press c.

In summary: separated by a hyphen means press together, separated by a space means release keys before continuing.

ShortcutAction
C-b ?List all shortcuts with explanations
C-b cCreate new tab
C-b ,Rename tab (Enter to finalize)
C-b lSwitch to previous active tab
C-b 3Switch to tab with index 3
C-b "Split window vertically
C-b %Split window horizontally
C-b zMaximize/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 dDetach session
C-b [Enter copy2 mode (enables you to scroll via arrow keys or mouse wheel)
qExit copy mode
C-<space>Begin copying (use arrow keys to select text)
M-wCopy 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:

  1. SSH into the remote server.
  2. Start a tmux session on the remote server.
  3. Start your app via command node server.js.
  4. Detach the tmux session via shortcut C-b d.
  5. Close the SSH connection.
  6. Later, SSH back into the remote server.
  7. Run tmux list-sessions to see that the session from step 2 is still running and get its session ID (probably zero).
  8. 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

  1. Drop-in replacement for SSH: Mosh. Simple as mosh user@host instead of ssh user@host (after installing Mosh locally and on remote server).

  2. See also traditional command line utils for copy/pasting, e.g. pbcopy and pbpaste on macOS and xclip and xsel on Linux.

  3. This is an alternative to foreground/background jobs. It is often recommended to use systemd to manage production server processes.