tmux 让你在一个终端窗口里挂多个 session / window / pane,断开 SSH 后
进程继续跑。任何远程开发都该用 tmux 或 screen。
安装
# Debian / Ubuntu
sudo apt install -y tmux
# macOS
brew install tmux
tmux -V # 确认 >= 3.0
最小配置 ~/.tmux.conf
# prefix 从 Ctrl-B 改成 Ctrl-A(更顺手;和 GNU readline 冲突的时候 set-key -r 解决)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 256 色 + 真彩
set -g default-terminal 'tmux-256color'
set -ga terminal-overrides ',xterm-256color:Tc'
# 鼠标(点击切 pane、滚轮翻历史、拖边界改大小)
set -g mouse on
# 窗口编号从 1 开始(数字键近一点)
set -g base-index 1
set -g pane-base-index 1
set -g renumber-windows on
# 历史滚动行数
set -g history-limit 100000
# 切 pane 用 vim 风格 hjkl
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# 切窗口用 Alt-数字
bind -n M-1 select-window -t :1
bind -n M-2 select-window -t :2
bind -n M-3 select-window -t :3
bind -n M-4 select-window -t :4
bind -n M-5 select-window -t :5
# split 用 | 和 -(更直观)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %
# 重载配置
bind r source-file ~/.tmux.conf \; display "Config reloaded"
# 拷贝模式用 vi 键位
setw -g mode-keys vi
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
# escape 时延(vim 模式必备,否则 ESC 后等半秒才响应)
set -sg escape-time 10
# 状态栏:左边 session 名,右边时间 + 主机
set -g status-style 'bg=#2b2d3a fg=#cdd6f4'
set -g status-left '#[fg=#a6e3a1,bold] #S #[default]'
set -g status-right '#[fg=#89b4fa]%H:%M #[fg=#fab387]%Y-%m-%d #[fg=#cba6f7]#h '
set -g status-left-length 30
set -g status-right-length 50
# 当前窗口高亮
setw -g window-status-current-style 'fg=#1e1e2e bg=#a6e3a1 bold'
setw -g window-status-current-format ' #I:#W '
setw -g window-status-format ' #I:#W '
日常用法
| 按键 | 动作 |
|---|---|
tmux |
新建一个 session |
tmux new -s mywork |
新建命名 session |
tmux ls |
列所有 session |
tmux a -t mywork |
attach 到指定 session |
| Prefix + c | 新 window |
| Prefix + , | 重命名当前 window |
| Prefix + n / p | 下一个 / 上一个 window |
| Prefix + 1..9 | 跳到第 N 个 window |
| Prefix + w | 列 window(可选) |
| Prefix + | | 横分 pane(按配置) |
| Prefix + - | 纵分 pane |
| Prefix + hjkl | 切 pane |
| Prefix + x | 关掉当前 pane |
| Prefix + d | detach(session 继续跑) |
| Prefix + [ | 进入滚动 / copy 模式 |
| q | 退出 copy 模式 |
| Prefix + ? | 当前所有快捷键 |
SSH + tmux
远程机器:
ssh server
tmux new -s work
# 干活...
# Ctrl-a d (detach),断开 SSH
下次:
ssh server
tmux a -t work # 之前的所有进程还在
更进一步——SSH 自动 attach:
# ~/.bashrc on server
if [[ -z "$TMUX" && -n "$SSH_TTY" ]]; then
tmux a -t main 2>/dev/null || tmux new -s main
fi
tmux + 项目
每个项目一个 session,每个 session 多个 window(如 "vim"、"shell"、"logs"):
tmux new -s myproj -d # 后台建
tmux rename-window -t myproj:0 'vim'
tmux send-keys -t myproj:0 'nvim .' Enter
tmux new-window -t myproj:1 -n 'shell'
tmux new-window -t myproj:2 -n 'logs'
tmux send-keys -t myproj:2 'tail -f log/app.log' Enter
tmux a -t myproj
这套脚本化的 session 启动用 tmuxinator / tmuxp 包管,写一份 YAML 配
所有 project layout。
拷贝粘贴
进入 copy 模式(Prefix + [),用 vi 键位 hjkl 移光标,v 开始选择,
y 拷贝。拷贝目标是 X11 clipboard(macOS 上 pbcopy、Wayland 上 wl-copy):
# Wayland 版本:
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'wl-copy'
# macOS 版本:
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy'
OSC 52 escape sequence 让 tmux 通过终端把内容传到本地剪贴板,
不需要 X11 forward:
set -g set-clipboard on
iTerm2 / Alacritty / WezTerm / kitty / 现代 Windows Terminal 都支持。
重命名 / 整理
tmux rename-session -t old new
tmux move-window -s myproj:3 -t myproj:1
tmux kill-session -t old
tmux kill-window -t myproj:3
踩过的坑
- 修改
.tmux.conf后没 reload:tmux source-file ~/.tmux.conf
或者 prefix-r(如果配置了)。 - 配置中颜色不显示:终端不支持真彩色(macOS 自带 Terminal);
换 iTerm2 / Alacritty / WezTerm。 escape-time不调,vim 里按 ESC 进 normal 模式有半秒延迟,
极其难用。设 10ms。- pane 拖动会触发 mouse;想用鼠标选文字复制到本地反而困难。
按住 Shift 拖能临时绕过 tmux 的 mouse capture,让终端模拟器原生处理。
登录后参与评论。