Skip to content

tmux 实战指南:终端复用的瑞士军刀

一次配置,长期受益 —— 从基础到进阶的完整指南

tmux cover


为什么需要 tmux?

作为开发者,你是否经历过这些困扰:

  • 终端窗口混乱:多个项目需要多个终端窗口,切换困难
  • SSH 断线丢失:远程连接断开后,工作状态全无
  • 环境恢复麻烦:每次打开都要重新配置窗口布局

tmux 是终端复用器(Terminal Multiplexer),它让你:

  • 一个终端窗口管理多个会话
  • SSH 断开后工作状态不丢失
  • 随时分离和恢复工作环境

AI 辅助开发的场景价值

在使用 Claude Code、Agent Teams 等 AI 辅助开发工具时,tmux 的价值更加凸显:

  • 并行监控 AI 团队:将主会话、代码编辑、测试运行分割在不同面板,实时监控多个 AI Agent 的工作进度
  • 持久化 AI 对话上下文:长时间运行的 AI 任务不会因为网络波动或终端关闭而中断
  • 快速切换项目上下文:不同项目使用独立 session,配合 AI 工具快速切换工作场景

延伸阅读:了解如何将 tmux 与 AI 团队协作结合,参见 Claude Code Agent Teams 指南


一、快速安装

bash
# macOS
brew install tmux

# 验证安装
tmux -V

二、核心概念速览

tmux 三层结构:
Session(会话)
 └── Window(窗口/标签页)
     └── Pane(面板/分割区域)

简单比喻:

  • Session = 一个独立的工作空间(比如"公司项目" vs "个人项目")
  • Window = 工作空间里的浏览器标签页
  • Pane = 标签页里用分隔线划分的区域

三、推荐配置文件

最小可用配置(速成版)

新手推荐:创建 ~/.tmux.conf,复制以下内容即可:

bash
# 前缀键改为 Ctrl+A(更顺手)
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# 启用鼠标(可以用点击和滚动)
set -g mouse on
# r 键重载配置
bind r source-file ~/.tmux.conf \; display "✅ Config reloaded!"

保存后执行:tmux source ~/.tmux.conf

完整推荐配置(进阶版)

当你熟悉基本操作后,可以升级到完整配置:

bash
# ========== 基础设置 ==========
# 前缀键改为 Ctrl+A(更顺手,不与 Vim 冲突)
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# 终端颜色支持
set -g default-terminal "tmux-256color"
set-option -sa terminal-features ",xterm*:RGB"

# 启用鼠标支持
set -g mouse on

# 窗口/面板编号从 1 开始(更符合键盘布局)
set -g base-index 1
setw -g pane-base-index 1

# 分屏继承当前路径
unbind %
bind | split-window -h -c "#{pane_current_path}"
unbind '"'
bind - split-window -v -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

# ========== Vim 风格导航 ==========
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# ========== 实用功能 ==========
# r 键重载配置
bind r source-file ~/.tmux.conf \; display "✅ Config reloaded!"

# 关掉响铃
set -g bell-action none

# 更长的历史记录
set -g history-limit 10000

# ========== 状态栏 ==========
set -g status-right "%H:%M"
set -g status-style bg=black,fg=white
set -g window-status-current-style "bold,underscore"

四、Shell 别名集成

添加到 ~/.zshrc~/.bashrc

bash
# tmux 别名
alias t='tmux attach 2>/dev/null || tmux new -s main'
alias tn='tmux new -s'
alias ta='tmux attach -t'
alias tl='tmux ls'
alias tk='tmux kill-session -t'

重新加载配置:

bash
source ~/.zshrc

五、十分钟掌握核心操作

启动流程

常用操作速查表

前缀键: Ctrl+A(下文记为 Prefix

操作快捷键说明
分屏
左右分屏Prefix + 竖线键垂直分割(Shift+\)
上下分屏Prefix + -水平分割
切换
Vim 风格切换Prefix h/j/k/l左/下/上/右
循环切换Prefix o在面板间循环
ZoomPrefix z当前面板全屏/复原
窗口
新建窗口Prefix c创建新窗口
切换窗口Prefix 数字跳转到指定窗口
会话
脱离会话Prefix dDetach(后台保留)
列出会话Prefix s选择/切换会话
其他
重载配置Prefix r重新加载配置文件
显示帮助Prefix ?列出所有快捷键

高频场景工作流

场景 1:开发 + 测试并行

1. t                          # 进入 tmux
2. cd ~/project               # 进入项目
3. Prefix |                   # 分屏
4. 左侧:vim code.py          # 编辑代码
5. 右侧:python test.py       # 运行测试
6. Prefix d                   # 脱离,后台继续运行

场景 2:多项目切换

bash
# 创建不同项目的 session
tmux new -s project-a
tmux new -s project-b

# 快速切换
Prefix s                      # 选择会话
# 或命令行
ta project-a

六、TPM 插件生态

安装 TPM(Tmux Plugin Manager)

bash
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

配置推荐插件

~/.tmux.conf 末尾添加:

bash
# ========== TPM 设置 ==========
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'    # 合理默认设置
set -g @plugin 'tmux-plugins/tmux-resurrect'   # 手动保存/恢复
set -g @plugin 'tmux-plugins/tmux-continuum'   # 自动持续保存
set -g @plugin 'tmux-plugins/tmux-yank'        # 剪贴板集成

# ========== resurrect 设置 ==========
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'

# ========== continuum 设置 ==========
set -g @continuum-restore 'on'           # 启动时自动恢复
set -g @continuum-save-interval '15'     # 每 15 分钟保存

# ========== 初始化 ==========
run '~/.tmux/plugins/tpm/tpm'

安装插件

保存配置后,执行以下命令安装插件:

bash
# 按 Ctrl+A 然后 Shift+I(大写 I)安装所有插件
# 或使用命令行:
~/.tmux/plugins/tpm/bin/install_plugins

预期效果:

  • 终端会显示安装进度
  • 安装完成后会提示 "TMUX environment reload completed"
  • 之后可以使用 Prefix + Ctrl+s 手动保存会话,Prefix + Ctrl+r 恢复

会话持久化

安装 continuum 后,你的 tmux 会话会自动保存:

  • 电脑崩溃后重启 → 自动恢复所有窗口和面板
  • SSH 断线重连 → 一键恢复工作状态

手动操作:

  • Prefix + Ctrl+s - 立即保存
  • Prefix + Ctrl+r - 立即恢复

七、故障排查速查表

问题解决方法
鼠标滚轮不工作Prefix [ 进入滚动模式,q 退出;或确认配置中 set -g mouse on
关闭终端后 session 消失Prefix d 脱离而非直接关窗口;用 t 重新连接
配置不生效执行 tmux source ~/.tmux.conf 或按 Prefix r
颜色显示异常确认 set -g default-terminal "tmux-256color"
tmux 完全卡死从另一个终端执行 tmux detach-client -akillall -9 tmux

紧急恢复

bash
# 杀死特定 session
tmux kill-session -t <name>

# 最后手段:杀死所有 tmux
killall -9 tmux

八、核心原则回顾

  • t 一键进入/恢复
  • Prefix d 脱离保留现场
  • 分屏用 |-
  • Vim 风格导航 h/j/k/l

参考资源


一次配置,长期受益。