Tmux 使用笔记
前言
最近我claude code 写代码比较多,电脑资源占用也紧张,遂尝试 TUI界面,目前 kitty + tmux + lazyvim
之所以选择tmux 而不选择 zellij ,因为tmux 更稳定。之前了解过,因为和kitty 的window,tab 功能重复,所以一直没有尝试,最近看有人推荐,花了两天强制使用了下,真的很好用,感觉比kitty 的layout 管理更好用。
还有就是感谢 ai,有了ai 使用新东西就是快
一、核心概念
1.1 三层结构
Tmux 采用三层模型组织终端:
Session(会话)
└─ Window 1(窗口)
├─ Pane 1(面板)
└─ Pane 2
└─ Window 2
└─ Pane 1
| 概念 | 说明 | 类比 |
|---|---|---|
| Session | 独立工作环境,断开后任务继续运行 | 一个完整工作区 |
| Window | 会话内的标签页 | 浏览器标签 |
| Pane | 窗口内的分割区域 | 分屏 |
1.2 与 Kitty 的对照
用习惯tmux 后,kitty 的没必要了解了
Kitty 的层级是:OS Window → Kitty Tab → Kitty Window,与 tmux 的 Session/Window/Pane 概念相近但不等价。
二、配置文件(~/.tmux.conf)
2.1 设置前缀键为 Ctrl+g
conf
# ========== 改前缀键为 Ctrl+g ==========
set -g prefix C-g
unbind C-b
# 连按两次 Ctrl+g 透传到内部程序(如 Neovim)
bind C-g send-prefix
# ========== Vim 风格 Pane 切换 ==========
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
2.2 配置热加载
- tmux 外部 :
tmux source-file ~/.tmux.conf - tmux 内部 :
Prefix + :然后输入source-file ~/.tmux.conf
三、快捷键速查(Prefix = Ctrl+g)
3.1 通用操作
| 快捷键 | 作用 |
|---|---|
Prefix + ? |
查看所有快捷键 |
Prefix + : |
进入命令行模式 |
Prefix + d |
断开 session(detach,进程继续运行) |
3.2 Session 管理
命令行操作:
bash
tmux ls # 列出所有 session
tmux a -t <name> # 接入指定 session(attach 简写)
tmux kill-session -t <name> # 从外部关闭 session
会话内切换:
| 快捷键 | 作用 |
|---|---|
Prefix + s |
交互式 session 列表(树状 + 预览,推荐) |
Prefix + L |
跳回上一个 session(项目间反复横跳高效) |
Prefix + : → kill-session |
非 detach 直接关闭当前 session |
Prefix + s → kill-session |
x 关闭session |
Prefix + $ |
rename session |
3.3 Window 管理
| 快捷键 | 作用 |
|---|---|
Prefix + c |
新建 window |
Prefix + & |
关闭当前 window |
Prefix + , |
rename当前 window |
Prefix + n / p |
下一个 / 上一个 window |
Prefix + 0~9 |
跳到指定编号 window |
Prefix + l |
跳回上一次活跃的 window |
Prefix + w |
交互式列出所有 session 与 window(推荐) |
3.4 Pane 管理
创建与关闭:
| 快捷键 | 作用 |
|---|---|
Prefix + % |
左右分割 |
Prefix + " |
上下分割 |
Prefix + x |
关闭当前 pane |
Prefix + Space |
循环切换布局 |
导航:
| 快捷键 | 作用 |
|---|---|
Prefix + h/j/k/l |
Vim 风格方向切换(需配置) |
Prefix + ↑↓←→ |
方向键切换相邻 pane |
Prefix + o |
顺时针循环切换 |
Prefix + ; |
跳回上一个 pane(编辑器/命令行往返高效) |
Prefix + q |
显示 pane 编号,按数字直接跳转 |
4. 配置文件
conf
# ========== 1. 改前缀键为 Ctrl+g ==========
set -g prefix C-g
unbind C-b
bind C-g send-prefix
# 开启支持鼠标
set -g mouse on
# ========== 窗口名固定 ==========
set -g automatic-rename off
set -g allow-rename off
# ========== copy-mode 选区高亮 ==========
# reverse 把当前单元格 fg/bg 互换,永远是最大对比;bold 加强可读性
set -g mode-style "reverse,bold"
# 1. 确保开启了 vi 模式
set-window-option -g mode-keys vi
# scrollback mode start
# 2. 在 copy-mode-vi 中配置类 Neovim 的半屏滚动
# -X 表示执行 tmux 命令
# halfpage-up 和 halfpage-down 是 tmux 内置的滚动指令
bind-key -T copy-mode-vi 'C-u' send-keys -X halfpage-up
bind-key -T copy-mode-vi 'C-d' send-keys -X halfpage-down
# 3. j 和 k 默认就是行滚动,但如果你想确保它们更灵敏
bind-key -T copy-mode-vi 'j' send-keys -X cursor-down
bind-key -T copy-mode-vi 'k' send-keys -X cursor-up
# scrollback mode end
# 1. 绑定 v 开始选择 (Visual 模式)
bind-key -T copy-mode-vi v send-keys -X begin-selection
# 2. y 复制 → 由 tmux-yank 接管(跨平台自动探测剪贴板工具 + OSC 52)
# ========== Vim 风格 Pane 切换 ==========
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# 放行终端控制序列:OSC 52(远程剪贴板回流)、kitty graphics/keyboard 协议
set -g allow-passthrough on
# 开启 OSC 52,SSH 远程剪贴板回流必备
set -s set-clipboard on
set-option -g update-environment "KITTY_PID KITTY_WINDOW_ID"
# ========== Plugins ==========
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'
set -g @plugin 'omerxx/tmux-floax'
set -g @plugin 'catppuccin/tmux#v2.1.3'
# ========== tmux-floax(浮动 pane) ==========
set -g @floax-bind 'f' # Prefix + f 唤起/隐藏浮窗(避开默认 p 与 previous-window 冲突)
set -g @floax-width '80%'
set -g @floax-height '80%'
set -g @floax-border-color 'magenta'
set -g @floax-text-color 'blue'
set -g @floax-change-path 'true' # 浮窗继承当前 pane 的 pwd
# ========== tmux-yank ==========
set -g @yank_selection 'clipboard' # 默认目标:系统剪贴板
set -g @yank_selection_mouse 'clipboard' # 鼠标选中也写剪贴板
set -g @yank_action 'copy-pipe' # 复制后保持 copy-mode(继续选/搜)
# ========== Catppuccin 主题 ==========
set -g @catppuccin_flavor 'mocha' # latte / frappe / macchiato / mocha
set -g @catppuccin_window_status_style 'rounded'
# 状态栏显示手动设定的 window name (#W),而不是默认的 pane title (#T)
set -g @catppuccin_window_text ' #{?window_zoomed_flag,[Z] ,}#W'
set -g @catppuccin_window_current_text ' #{?window_zoomed_flag,[Z] ,}#W'
# 状态栏(左空,右:copy-mode 徽章 + 应用 + session + 时间)
set -g status-right-length 100
set -g status-left-length 100
set -g status-left ''
# 进 copy-mode 时左侧弹出红底徽章;非 copy-mode 不占位
set -g status-right '#{?pane_in_mode,#[bg=#f38ba8 fg=#1e1e2e bold] [C] COPY #[default] ,}#{E:@catppuccin_status_application}'
# 模式切换时立刻刷新状态栏(不等 status-interval)
set-hook -g pane-mode-changed 'refresh-client -S'
set -g status-interval 1
set -agF status-right '#{E:@catppuccin_status_session}'
set -agF status-right '#{E:@catppuccin_status_date_time}'
set -g @catppuccin_date_time_text '%Y-%m-%d %H:%M'
# resurrect: 恢复时也恢复 neovim session(可选,需 lazyvim 配 persistence)
set -g @resurrect-strategy-nvim 'session'
# continuum: 自动保存间隔(分钟),默认 15
set -g @continuum-restore 'on' # tmux 启动时自动恢复
set -g @continuum-save-interval '15'
# ========== 必须放最后一行 ==========
run '~/.tmux/plugins/tpm/tpm'
# ========== 覆盖 catppuccin 的边框样式(必须在 tpm 之后,否则被插件覆盖)==========
# 进 copy-mode 的 pane 边框变红加粗,多 pane 时一眼锁定
set -g pane-active-border-style "#{?pane_in_mode,fg=#f38ba8 bold,fg=#b4befe}"