Yangchengyu
← 返回博客

tmux 快速参考

2026-07-02 · 工具文档

1. Core Concepts

tmux 是终端复用器,在服务器端运行。SSH 断开或本地电脑关机,tmux 内的程序继续运行

三层结构:会话 Session(顶层容器)包含多个 窗口 Window(类似浏览器标签),每个窗口可分割为多个 窗格 Pane(独立终端区域)。

2. Installation

PlatformCommand
macOS (Homebrew)brew install tmux
Ubuntu / Debiansudo apt install -y tmux
CentOS / RHELsudo yum install -y tmux
Alpine Linuxapk add tmux

3. Session Management

CommandAction
tmux new -s nameCreate named session
tmux lsList all sessions
tmux a -t nameAttach to existing session
tmux new -A -s nameAttach or create
tmux kill-session -t nameKill session completely

4. Key Bindings

Prefix key = Ctrl+a (press Ctrl+a, release, then press the following key)

KeyActionKeyAction
|Split vertically"Split horizontally
ArrowsNavigate panesqShow pane numbers
cNew windowdDetach session
n / pNext / Prev window&Close current window
1-9Select windowwWindow list select
!Break pane to windowzZoom / restore pane
xClose current pane[Copy mode
Ctrl+dExit tmux?Show all bindings

5. Remote Daemon Workflow

StepActionNote
1ssh user@serverConnect to server via SSH
2tmux new -s workCreate tmux session
3python train.pyRun task inside session
4Ctrl+a dDetach (keeps running)
5exitSafely disconnect SSH / close laptop
6ssh user@server
tmux a -t work
Reattach later

6. Pane Management

ActionMethodNote
Navigate panesCtrl+a oCycle through panes
Close current paneCtrl+a xPrompts confirm, press y
Force kill paneCtrl+a :kill-paneWhen pane is frozen
Zoom / restoreCtrl+a zFullscreen / restore layout
Kill remote panetmux kill-pane -t 0:0.2Run from native terminal

7. Text Copying

Recommended: set -g mouse off, keep native terminal mouse selection.

MethodOperation
iTerm2 native (recommended)Drag select → Cmd+c
Vi copy modeCtrl+a [v select → y copy
Option+mouse (fallback)Hold Option while dragging, bypass tmux intercept

8. Tips

Reload config
After editing ~/.tmux.conf, press Ctrl+a : and type source-file ~/.tmux.conf.
Long-running task insurance
Use nohup cmd > out.log 2>&1 & then disown, prevents process death if tmux closes unexpectedly.
SSH keepalive
Add ServerAliveInterval 60 to ~/.ssh/config to prevent idle disconnects.
Cross-machine consistency
Sync local ~/.tmux.conf to remote via scp, keep key bindings consistent.