Please enable Javascript to view the contents

Tmux 最佳实践:Mac 用户的终端效率指南

 ·  ☕ 5 分钟 · 👀... 阅读

Tmux 最佳实践:Mac 用户的终端效率指南

没用过 tmux?你损失的可不只是时间。
本文面向 macOS 用户,iTerm2 + tmux 是 Mac 终端的黄金组合。


1. 为什么要用 tmux?

一句话: session 可以随时断开重连,进程不会丢。

  • 远程服务器 SSH 断线?重连后 session 还在,运行的任务继续执行
  • 多窗口切换、不同项目分屏管理、复用配置——这些才是真正的时间节省

2. 核心概念(先搞懂再动手)

概念 作用
Session 最外层容器,一个 session = 一个工作环境
Window 类似浏览器标签页,一个 session 多个 window
Pane 窗口内的分屏,一个 window 多个 pane

操作口诀Ctrl+b 是前缀(默认),所有快捷键之前先按它。


3. 必备快捷键(每天至少用 100 次)

Session 管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 创建新 session
tmux new -s work

# 列出所有 session
tmux ls

# detach(退出但保持运行): Ctrl+b d

# 重新 attach
tmux attach -t work

# 杀掉 session
tmux kill-session -t work

Window 操作

1
2
3
4
5
Ctrl+b c          # 新建 window
Ctrl+b n          # next window
Ctrl+b p          # previous window
Ctrl+b 0-9        # 跳到指定编号 window
Ctrl+b ,          # 重命名当前 window

Pane 操作(最常用)

1
2
3
4
5
6
7
8
Ctrl+b %          # 垂直分屏
Ctrl+b "          # 水平分屏
Ctrl+b 方向键     # 切换 pane
Ctrl+b z          # 最大化/还原当前 pane
Ctrl+b x          # 关闭当前 pane
Ctrl+b {          # 与左边的 pane 交换
Ctrl+b }          # 与右边的 pane 交换
Ctrl+b Space      # 切换分屏布局

4. 实用配置(.tmux.conf)

别用默认配置,改配置文件才能真正提效。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# ~/.tmux.conf

# === 前缀改成 Ctrl+a(比 Ctrl+b 更顺手)===
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# === 开启鼠标支持(强烈推荐)===
set -g mouse on

# === 起始窗口编号设为 1(不是 0)===
set -g base-index 1

# === 窗口自动重编号(关闭窗口后序号重排)===
set -g renumber-windows on

# === 状态栏配置 ===
set -g status-bg black
set -g status-fg white
set -g status-left "#S"  # 显示 session 名称
set -g status-right "%Y-%m-%d %H:%M"

# === 启用活动监控(其他 pane 有活动时显示提醒)===
setw -g monitor-activity on
set -g visual-activity on

# === 更快的响应速度 ===
set -g escape-time 0

# === 复制模式配置(Vi 风格)===
setw -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X selection
bind-key -T copy-mode-vi y send-keys -X copy-selection

# === macOS 特有优化 ===

# 启用剪贴板集成(macOS)
set -g set-clipboard on

# 状态栏时间格式(macOS 风格)
set -g status-right "%Y-%m-%d %H:%M"

# 终端类型(确保 vim 等工具正确识别)
set -g default-terminal "screen-256color"

加载配置tmux source-file ~/.tmux.conf 或重启 tmux。

💡 macOS 小技巧:在 iTerm2 中,建议将 Option 键设置为 Esc+,这样 Option+方向键 可以在 tmux 中使用 vi 风格的词级跳转。


5. 高阶技巧

5.1 复制粘贴的正确姿势

  1. 进入复制模式:Ctrl+b [
  2. vi 键导航(h j k l 或方向键)
  3. v 开始选中,y 复制
  4. Ctrl+b ] 粘贴

5.2 同步分屏(批量操作多终端)

1
2
3
4
5
# 开启同步
Ctrl+b :setw synchronize-panes on

# 关闭
Ctrl+b :setw synchronize-panes off

场景:同时在多台服务器执行相同命令。

5.3 窗口配对(Pairing)

1
2
# 水平分屏后,两个 pane 同步切换 window
Ctrl+b :setw synchronize-panes on

5.4 快速布局

1
2
3
4
Ctrl+b Alt+1   # 垂直平铺
Ctrl+b Alt+2   # 水平平铺
Ctrl+b Alt+3   # 主窗口居中
Ctrl+b Alt+4   # 铺满

5.5 插件推荐(Tmux Plugin Manager)

安装 TPM:

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

必装插件

  • tmux-resurrect:保存恢复 session 状态(重启不丢)
  • tmux-continuum:自动保存恢复

配置:

1
2
3
4
5
6
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# 保存:Ctrl+b Ctrl+s
# 恢复:Ctrl+b Ctrl+r

6. macOS 生态集成

6.1 iTerm2 + tmux = 黄金组合

功能 iTerm2 原生 tmux 集成
状态栏 美观原生 需要配置
搜索 Cmd+F tmux 复制模式
鼠标支持 完美支持 需配置
多终端管理 一般 强大

推荐设置

  1. iTerm2 → Preferences → Profiles → Terminal
  2. 勾选 Set locale variables automatically
  3. 终端类型设为 xterm-256color

6.2 使用 tmuxinator 管理项目(macOS 强烈推荐)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 安装
brew install tmuxinator

# 创建项目配置
tmuxinator new myproject

# 编辑配置 (~/.tmuxinator/myproject.yml)
# 示例:
# name: myproject
# root: ~/Projects/myapp
# windows:
#   - editor: vim
#   - server: npm run dev
#   - test: npm test

# 启动
tmuxinator start myproject

场景:每天早上 mux start work,自动打开所有项目窗口。

6.3 macOS 剪贴板集成

1
2
3
4
5
6
# 安装 reattach-to-user-namespace(让 tmux 访问系统剪贴板)
brew install reattach-to-user-namespace

# 添加到配置
set -g set-clipboard on
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"

7. 我的日常 workflow

┌─────────────────────────────────────┐
│ work session                        │
├───────────────┬─────────────────────┤
│ $ vim server.py │ $ python -m http.server │
│               │                     │
├───────────────┴─────────────────────┤
│ $ node app.js                       │
└─────────────────────────────────────┘
  • Window 1:后端开发(代码 + 服务)
  • Window 2:前端/测试
  • Window 3:日志/监控
  • Window 4:杂项(查文档、临时命令)

8. 常见坑

解决方案
分屏后文字显示不全 Ctrl+b z 最大化 pane 查看
复制模式选中不了 确认 setw -g mode-keys vi
鼠标无法选择 检查 set -g mouse on
配置不生效 tmux source-file ~/.tmux.conf

9. 快速入门命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# ========== macOS 安装(推荐使用 Homebrew)==========

# 安装 Homebrew(如果没有)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 tmux
brew install tmux

# 验证版本
tmux -V

# 常用操作流程
tmux new -s myproject      # 创建 session
Ctrl+b c                   # 新建 window
Ctrl+b "                   # 水平分屏
Ctrl+b %                   # 垂直分屏
Ctrl+b 方向键              # 切换 pane
Ctrl+b d                   # detach
tmux attach -t myproject   # 重新进入

macOS 特有的优化

推荐终端:iTerm2(内置 tmux 集成)

1
2
3
4
5
# iTerm2 中启用 tmux 集成(推荐)
# iTerm2 → Preferences → Advanced → tmux
# 勾选 "Enable tmux integration"

# 好处:iTerm2 原生渲染,状态栏更美观,鼠标支持更流畅

Homebrew 升级 tmux

1
2
brew upgrade tmux      # 升级到最新版本
brew cleanup tmux      # 清理旧版本

结语

tmux 的学习曲线不陡,但真正用起来需要配置+习惯

核心就三点:

  1. 改前缀(Ctrl+a
  2. 配鼠标
  3. 装插件

坚持一周,你会觉得以前没用 tmux 的自己像个原始人。


VictorHong
作者
VictorHong
🔩工具控,⌨️ 后端程序员,🧪AI 探索者