Tmux 高效配置:一键分屏 + 状态栏显示所有分屏的目录与 Claude 状态
配置文件:tmux.conf,前缀键
Ctrl+a,热重载Alt+r。
配套脚本:tmux-config/ 目录,可直接拷贝到新机器。
一、快捷键速查
全部免前缀(Alt 单键),覆盖日常操作:
| 快捷键 | 功能 |
|---|---|
Alt+h | 左右分屏 |
Alt+v | 上下分屏 |
Alt+x | 关闭当前窗格 |
Alt+z | 窗格全屏 / 恢复 |
Alt+, | 切换布局方向(左右↔上下) |
Alt+. | 窗格交换位置(循环轮转) |
Alt+← ↓ ↑ → | 窗格间移动 |
Alt+Shift+← ↓ ↑ → | 调整窗格大小 |
Alt+c | 新建窗口 |
Alt+e | 重命名窗口 |
Alt+n / Alt+p | 下一个 / 上一个窗口 |
Alt+1 ~ Alt+5 | 跳到指定窗口 |
Alt+w | 窗口列表 |
Alt+b | 进入复制模式 |
Alt+y | 粘贴 |
Alt+d | 分离会话 |
Alt+r | 重载配置 |
前缀备选(Ctrl+a):h v 分屏、←↓↑→ 移动、1-9 窗口、c 新建、, 重命名、d 分离、b 复制模式、y 粘贴。
复制粘贴到系统剪贴板
前提:sudo apt install xclip
Alt+b → 进入复制模式
翻到开头(k/Ctrl+u)
v → 开始可视选择
移动光标跨越屏幕 → 选中多屏内容
y → 复制(同时写入系统剪贴板)
Alt+y 或到外面 Ctrl+v → 粘贴
复制模式下 y 同时写入 tmux 缓冲区和系统剪贴板(通过 xclip)。不用 Shift+拖选——那只能选当前屏幕可见内容。跨屏复制必须用复制模式。
二、状态栏架构
布局一览
🔔 2 session 1:dev 2:srv W1:上:dev🟡 下:log🟢 │ W2:左:srv🔵 右:test💻 01-01 12:00
↑ ↑ ↑ ↑
status-left 窗口列表 status-right 时间
待确认计数 简洁无干扰 所有窗口的分屏方位、目录、状态
三层组件
all-windows-panes.sh (每 5s 读取) ← 渲染层
↑
@claude-state (pane 级别 user option) ← 数据层
↑
6 个 hook 脚本 (Claude Code 事件触发) ← 写入层
| 层 | 文件 | 做什么 |
|---|---|---|
| 渲染 | ~/.tmux/all-windows-panes.sh | 遍历所有窗口/pane,输出方位+目录+状态 |
| 渲染 | ~/.tmux/pending-alert.sh | 统计待确认 pane 数量,输出 🔔 N |
| 配置 | ~/.tmux.conf | 左中右三段布局,5 秒刷新 |
| 数据 | hook 脚本 × 6 | 更新 @claude-state,不碰 status bar |
核心设计:读写分离。 hook 只写数据,脚本只读数据,永不会覆盖状态栏。
每个 pane 独立显示状态
非 Claude pane 根据命令自动匹配图标:
| 命令 | 显示 |
|---|---|
bash / zsh | 💻shell |
vim / nvim | 📝vim |
python | 🐍python |
node | 🟢node |
git | 🔀git |
docker | 🐳docker |
Claude pane 读 @claude-state,每个 pane 独立维护(通过 $TMUX_PANE 环境变量区分):
| 状态 | 显示 | 触发事件 |
|---|---|---|
| 工作中 | 🔵运行 | UserPromptSubmit / PreToolUse |
| 待确认 | 🟡待确认 | PermissionRequest / Notification |
| 空闲 | 🟢空闲 | Stop / SessionStart |
分屏方位命名
基于 pane_top / pane_left 坐标自动计算:
| 布局 | 命名 |
|---|---|
| 左右 2 栏 | 左 右 |
| 上下 2 栏 | 上 下 |
| 左主 + 右 2 | 左 上右 下右 |
| 田字 2×2 | 上左 上右 下左 下右 |
| 3 栏 | 左 中 右 |
| 4+ 栏 | 左1 左2 … |
智能压缩
窗口 > 3 个时自动切换紧凑模式:
| 窗口数 | 格式 | 示例 |
|---|---|---|
| ≤3 | 完整 | W1:上:dev 🟡 下:log 🟢 |
| >3 | 紧凑 | W1:dev 🟡, log 🟢 |
多 Claude 同时确认 — 四层告警
当多个窗口/分屏的 Claude 同时需要确认:
| 层 | 机制 | 效果 |
|---|---|---|
| ① | pending-alert.sh → status-left | 🔔 2 红色计数 |
| ② | all-windows-panes.sh → status-right | 每个 pane 显示 🟡待确认 |
| ③ | pane 边框变黄 | pane-border-style fg=yellow,bold |
| ④ | 终端响铃 + 窗口 ! 标记 | tmux bell + monitor-bell |
即使你在窗口 2 工作、窗口 1 的 3 个 Claude 同时待确认,一行看清:
🔔 3 session 2:server W1:上:dev🟡 下:log🟡 左:test🟡 │ W2:server🔵 12:00
待确认恢复 — 三重机制
| 机制 | 触发 | 延迟 |
|---|---|---|
| Pane 切换 | Alt+方向键 切到待确认 pane | 即时(自动清除) |
| PreToolUse | 点击”允许”按钮 | 即时(hook + refresh) |
| 时间戳兜底 | 活动 1 秒 / 非活动 5 秒 | 最多 5 秒(在别的 pane 时) |
Pane 切换:切到显示 🟡待确认 的 pane 时,on-pane-switch.sh 自动清除待确认状态为 🔵运行——因为你过去就是为了处理确认。
Alt+↓ 切到待确认 pane → on-pane-switch.sh → @claude-state=🔵运行 + refresh-client → 状态栏即时切换
PreToolUse:点击确认菜单 → Claude 执行工具 → PreToolUse hook → set-working.sh 写入 🔵运行 + refresh-client。
点击"允许" → PreToolUse → set-working.sh → @claude-state=🔵运行 + refresh-client → 状态栏即时切换
注意:修改
settings.json后必须重启 Claude Code,hooks 在会话启动时加载。
时间戳兜底:活动 pane 1 秒 / 非活动 5 秒自动过期,防止极端情况僵死。
# all-windows-panes.sh 中的过期保护
if [[ "$cs" == *"待确认"* ]]; then
elapsed=$(($(date +%s) - $(tmux display -p -t "$pid" '#{@claude-pending-since}')))
[ "$active" = "1" ] && max=1 || max=5 # 活动 1s / 非活动 5s
[ "$elapsed" -gt "$max" ] && cs="🔵运行"
fi
所有 hook 脚本末尾都调用了 tmux refresh-client -S,~/.tmux.conf 中 pane 切换也会刷新:
# 切换 pane 时:自动清除待确认 + 刷新状态栏
set-hook -g after-select-pane 'run-shell "~/.tmux/on-pane-switch.sh #{pane_id}"'
三、安装
3.1 基础配置(快捷键 + 分屏)
cp tmux-config/tmux.conf ~/.tmux.conf
mkdir -p ~/.tmux
cp tmux-config/scripts/all-windows-panes.sh ~/.tmux/
cp tmux-config/scripts/pending-alert.sh ~/.tmux/
cp tmux-config/scripts/pane-status.sh ~/.tmux/
cp tmux-config/scripts/on-pane-switch.sh ~/.tmux/
chmod +x ~/.tmux/*.sh
在 tmux 内 Alt+r 重载。状态栏右侧应出现分屏信息。
3.2 Claude Code 状态联动(可选)
mkdir -p ~/.claude/hooks
cp tmux-config/hooks/set-*.sh ~/.claude/hooks/
chmod +x ~/.claude/hooks/set-*.sh
编辑 ~/.claude/settings.json,添加 hooks:
{
"hooks": {
"SessionStart": [
{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/set-idle.sh" }] }
],
"UserPromptSubmit": [
{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/set-working.sh" }] }
],
"Stop": [
{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/set-idle.sh" }] }
],
"PermissionRequest": [
{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/set-waiting.sh" }] }
],
"PreToolUse": [
{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/set-working.sh" }] }
]
}
}
3.3 前置要求
- tmux ≥ 3.0
- 终端支持 256 色 + emoji(推荐 Nerd Font)
- Claude Code(仅 hooks 需要)
四、Hook 脚本源码
set-waiting.sh——待确认时,状态 + 时间戳 + 黄边框 + 响铃四道保险:
#!/bin/bash
echo "waiting" > "/tmp/claude-state-${USER}.txt"
if [ -n "${TMUX:-}" ] && [ -n "${TMUX_PANE:-}" ]; then
tmux set-option -p -t "$TMUX_PANE" @claude-state "🟡待确认"
tmux set-option -p -t "$TMUX_PANE" @claude-pending-since "$(date +%s)"
tmux set-option -p -t "$TMUX_PANE" pane-border-style 'fg=yellow,bold'
tmux bell -t "$TMUX_PANE"
tmux refresh-client -S # 强制立即刷新状态栏
fi
set-working.sh / set-idle.sh 同样结构,状态切换时自动恢复边框 + refresh-client 立即刷新。
五、核心渲染脚本
all-windows-panes.sh 核心逻辑(精简版):
#!/bin/bash
# 1. 遍历所有窗口
for win in $(tmux list-windows -F "#{window_index}"); do
# 2. 收集每个 pane 的坐标 (pane_top / pane_left) 和命令
panes=$(tmux list-panes -t "$win" -F "pane_id|top|left|cmd|path")
# 3. 计算方位名(上/下/左/右...)
pos=$(get_position "$top" "$left" "$tops" "$lefts")
# 4. 读 @claude-state 或匹配命令图标
state=$(get_state "$cmd")
# 5. 窗口 > 3 → 紧凑模式(去方位名,逗号分隔)
done
pending-alert.sh——统计所有 pane 待确认数量:
#!/bin/bash
count=0
for pid in $(tmux list-panes -a -F "#{pane_id}"); do
state=$(tmux display -p -t "$pid" '#{@claude-state}')
[[ "$state" == *"待确认"* ]] && count=$((count + 1))
done
[ "$count" -gt 0 ] && echo -n "#[fg=yellow,bold]🔔 ${count} #[fg=colour245]│ "
六、完整配置文件
~/.tmux.conf:
# ── 基础 ──────────────────────────────────────
set -g mouse on
set -g base-index 1
set -g pane-base-index 1
set -g history-limit 50000
set -g default-terminal "screen-256color"
set -g escape-time 0
set -g focus-events on
set -g renumber-windows on
# ── 前缀键 ────────────────────────────────────
set -g prefix C-a
unbind C-b
bind C-a send-prefix
bind C-a last-window
# ── 免前缀 Alt 快捷键 ─────────────────────────
# 分屏
bind -n M-h split-window -h -c "#{pane_current_path}"
bind -n M-v split-window -v -c "#{pane_current_path}"
bind -n M-x kill-pane
bind -n M-b copy-mode # Alt+b 进入复制模式
bind -n M-y paste-buffer # Alt+y 粘贴
bind -n M-z resize-pane -Z
bind -n M-, next-layout # Alt+, 切换布局方向
# 窗格移动
bind -n M-Left select-pane -L
bind -n M-Down select-pane -D
bind -n M-Up select-pane -U
bind -n M-Right select-pane -R
# 窗格调整
bind -n M-S-Left resize-pane -L 5
bind -n M-S-Down resize-pane -D 5
bind -n M-S-Up resize-pane -U 5
bind -n M-S-Right resize-pane -R 5
# 窗格交换
bind -n M-. swap-pane -D # Alt+. 窗格轮转交换位置
# 窗口
bind -n M-c new-window -c "#{pane_current_path}"
bind -n M-e command-prompt -I "#W" "rename-window '%%'"
bind -n M-n next-window
bind -n M-p previous-window
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
bind -n M-w choose-window
bind -n M-d detach-client
# 重载
bind -n M-r source-file ~/.tmux.conf \; display-message "✓ tmux.conf 已重载"
bind r source-file ~/.tmux.conf \; display-message "✓ tmux.conf 已重载"
# 前缀备选
bind h split-window -h -c "#{pane_current_path}"
bind v split-window -v -c "#{pane_current_path}"
bind \\ split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind | split-window -h -c "#{pane_current_path}"
# ── 状态栏 ────────────────────────────────────
set -g status-position bottom
set -g status-style bg=colour236,fg=white
set -g status-justify left
set -g status-interval 5
# 左侧:待确认计数 + 会话名
set -g status-left "#(~/.tmux/pending-alert.sh)#[fg=cyan]#{session_name} "
set -g status-left-length 80
# 中间:窗口列表
set -g window-status-format " #I:#W "
set -g window-status-current-format "#[fg=black,bg=colour114,bold] #I:#W #[bg=colour236,fg=white]"
# 右侧:所有窗口的分屏信息 + 时间
set -g status-right "#(~/.tmux/all-windows-panes.sh)#[fg=colour245] %Y-%m-%d %H:%M"
set -g status-right-length 500
# ── 边框与铃声 ────────────────────────────────
set -g pane-active-border-style fg=green,bg=default
set -g monitor-bell on
set -g bell-action other
set -g visual-bell off
# ── 系统剪贴板 ────────────────────────────────
# 复制模式下 y 同时写入系统剪贴板(需要 apt install xclip)
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard 2>/dev/null"
# ── Pane 切换自动处理 ────────────────────────
set-hook -g after-select-pane 'run-shell "~/.tmux/on-pane-switch.sh #{pane_id}"'
七、常见问题
| 问题 | 解决 |
|---|---|
| emoji 乱码 | 安装 Nerd Font 或系统 emoji 字体 |
状态栏显示原始文本 #(...) | 检查脚本路径和可执行权限 |
| 分屏命名不对 | 基于坐标计算,Alt+r 重载刷新 |
| 紧凑模式触发太早/晚 | 编辑脚本中 total_windows -le 3 数字 |
不想要窗口号前缀 W1: | 编辑脚本删除 W${win_idx}: |
| 多 Claude 状态混乱 | 检查 $TMUX_PANE 环境变量是否存在 |
💡 配套脚本导出目录:tmux-config/,包含所有脚本和
SETUP.md安装指南,可直接拷贝到新机器。
