Tmux

What

Tmux is a terminal multiplexer, which allows you manage multiple terminal sessions, windows, and panes within a single terminal window.

In tmux’s hierarchy, a shell could have multiple sessions, a session could have multiple windows, a window could have multiple panes.

We can access specified session, window or pane by session:window.pane if there’s multiple sessions, windows or panes.

As this example, 0 and display is two sessions, which have two windows respectively. 0:0 has three panes, which can be accessed by 0:0.0 ~ 0:0.2.

Why

  • Run multiple shells in one terminal.
  • Keep program running even if SSH is disconnected!
flowchart TD
    subgraph NoTmux["Without tmux"]
        A[SSH Client] <--> B[sshd]
        B --> C[shell]
        C --> D[program]
    end

    subgraph Tmux["With tmux"]
        E[SSH Client] <--> F[sshd]
        F --> G[shell]
        G --> H[tmux client]
        
        subgraph "tmux server (on server)"
            I[shell]
            I --> J[program]
        end
    end

    style NoTmux fill:#fef3c7,stroke:#f59e0b,stroke-width:2px
    style Tmux fill:#dbeafe,stroke:#3b82f6,stroke-width:2px

  • Without tmux: Shell pty will disappear once SSH is disconnected, then program will be killed.
  • With tmux: Only tmux client will die when SSH is disconnected but tmux server is still run in the server so that the program will keep running.
・・ ・ー・・ ーーー ・・・ー ・ ー・ーー ーーー ・・ー

There are some useful commands and shortcuts used frequently. For more details, man and Google is your friends!

CLI

Create && Attach

  • tmux, tmux new: Create a anonymous session (0)
  • tmux new -s work: Create a session named “work”
  • tmux attach: Attach to the latest session.
  • tmux attach -t work: Attach specified session “work”
  • tmux ls: List all sessions
    • tmux list-windows -t build
    • tmux list-panes -t build:0
  • tmux new -s build -d: Create a session background
    • tmux send -t build 'make' Enter: Enter “make” to the session “build”, which is simulate keyboard and not dependent shell attach. This could be used for automation of background tasks.

Delete

  • tmux kill-session -t work: Delete specified session “work”
  • tmux kill-server: Delete all sessions

Shortcuts

By default, prefix is Ctrl + b which is a bit uncomfortable…
However, we could edit ~/.tmux.conf to customize.

set -g prefix C-a
unbind C-b
bind C-a send-prefix

Pane

OperationShortcut
Split pane verticallyPrefix %
Split pane horizontallyPrefix “
Close panePrefix x
Navigate between panesPrefix ← / ↑ / ↓ / →
Resize panePrefix Ctrl+← / Ctrl+→ / Ctrl+↑ / Ctrl+↓
Show pane numbersPrefix q

Window

OperationShortcut
New windowPrefix c
Rename windowPrefix ,
Switch to next windowPrefix n
Switch to previous windowPrefix p
Switch to specific windowPrefix 0–9
List all windowsPrefix w
Close windowexit or Prefix &

Session

OperationShortcut
Detach from sessionPrefix d
Switch between sessionsPrefix s
Rename sessionPrefix $

Command Mode

Prefix :
Then we can enter tmux command (tmux is not needed), such as:

  • New-window
  • Split-window -h
  • Rename-session work
  • List-keys
  • List-windows
  • List-panes
  • And so on.

More Customization

To do customization, just edit ~/.tmux.conf.

Rule

  • bind <key> <command>: Press key after prefix.
  • bind -n <key> <command>: Take effect once press key without need of pressing prefix
bind -n C-h select-pane -L
bind -n C-l select-pane -R
bind -n C-j select-pane -D
bind -n C-k select-pane -U

Split Windows

bind | split-window -h
bind - split-window -v

Make it a more convenient to use!