Tmux 终端复用进阶¶
为什么要学 Tmux 进阶¶
Tmux 是终端复用器的标准工具,但大多数人只用到了基础的窗口分屏功能。进阶用法包括自定义配置美化、插件系统、会话持久化、脚本化工作空间、远程协作等。掌握这些技能后,你可以构建高效的终端工作流——SSH 断线恢复会话、一键搭建项目开发环境、多终端同步操作,极大提升服务器操作和本地开发效率。
核心概念¶
| 概念 | 白话解释 | 用途 |
|---|---|---|
| Session | 会话 | 一组窗口的集合,可断线恢复 |
| Window | 窗口 | 一个完整的终端视图 |
| Pane | 面板 | 窗口内的分屏区域 |
| Prefix Key | 前缀键 | 激活 tmux 命令的按键组合 |
| TPM | 插件管理器 | 安装和管理 tmux 插件 |
| Status Bar | 状态栏 | 底部信息显示区域 |
安装配置¶
安装¶
# Ubuntu/Debian
sudo apt install tmux
# macOS
brew install tmux
# 从源码(获取最新版)
git clone https://github.com/tmux/tmux.git
cd tmux && sh autogen.sh && ./configure && make && sudo make install
现代化配置¶
# ~/.tmux.conf
# 修改前缀键为 Ctrl-a(更顺手)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 基础设置
set -g mouse on # 启用鼠标
set -g base-index 1 # 窗口从1开始编号
setw -g pane-base-index 1 # 面板从1开始
set -g renumber-windows on # 关闭窗口后重新编号
set -g history-limit 50000 # 历史行数
set -g escape-time 0 # 减少 ESC 延迟
set -g focus-events on # 聚焦事件
set -g status-interval 5 # 状态栏刷新间隔
# 24位真彩色
set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",xterm-256color:RGB"
# 分屏快捷键(更直觉)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %
# Vim 式面板导航
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# 面板大小调整
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# 快速重载配置
bind r source-file ~/.tmux.conf \; display "配置已重载"
# Vi 模式复制
setw -g mode-keys vi
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "xclip -selection clipboard"
快速上手¶
基础操作速查¶
会话:
tmux new -s dev 创建名为 dev 的会话
tmux attach -t dev 附加到 dev 会话
tmux ls 列出所有会话
prefix + d 分离会话
prefix + $ 重命名会话
prefix + s 会话列表(可切换)
窗口:
prefix + c 新建窗口
prefix + , 重命名窗口
prefix + n/p 下/上一个窗口
prefix + 1-9 跳转到窗口
prefix + w 窗口列表
prefix + & 关闭窗口
面板:
prefix + | 水平分屏(自定义)
prefix + - 垂直分屏(自定义)
prefix + h/j/k/l 面板导航
prefix + z 面板全屏切换
prefix + x 关闭面板
prefix + q 显示面板编号
prefix + Space 切换布局
进阶用法¶
安装插件管理器 TPM¶
# ~/.tmux.conf 底部添加
# 插件列表
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 'catppuccin/tmux' # 主题美化
set -g @plugin 'tmux-plugins/tmux-cpu' # CPU 显示
# 插件配置
set -g @resurrect-capture-pane-contents 'on'
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
# 初始化 TPM(保持在最后一行)
run '~/.tmux/plugins/tpm/tpm'
状态栏美化¶
# Catppuccin 主题配置
set -g @catppuccin_flavor "mocha"
set -g @catppuccin_window_status_style "rounded"
set -g @catppuccin_status_modules_right "application session date_time"
set -g @catppuccin_status_modules_left ""
set -g @catppuccin_date_time_text "%H:%M"
脚本化工作空间¶
#!/bin/bash
# ~/scripts/dev-workspace.sh
# 一键创建开发环境
SESSION="dev"
tmux new-session -d -s $SESSION -c ~/projects/myapp
# 窗口1:编辑器
tmux rename-window -t $SESSION:1 "editor"
tmux send-keys -t $SESSION:1 "nvim ." C-m
# 窗口2:服务器
tmux new-window -t $SESSION:2 -n "server" -c ~/projects/myapp
tmux send-keys -t $SESSION:2 "npm run dev" C-m
# 窗口3:终端(分两个面板)
tmux new-window -t $SESSION:3 -n "terminal" -c ~/projects/myapp
tmux split-window -h -t $SESSION:3 -c ~/projects/myapp
tmux send-keys -t $SESSION:3.1 "git status" C-m
tmux send-keys -t $SESSION:3.2 "docker compose ps" C-m
# 窗口4:日志
tmux new-window -t $SESSION:4 -n "logs" -c ~/projects/myapp
tmux send-keys -t $SESSION:4 "tail -f logs/app.log" C-m
# 切回第一个窗口
tmux select-window -t $SESSION:1
# 附加到会话
tmux attach-session -t $SESSION
会话持久化与恢复¶
# tmux-resurrect 自动保存和恢复
# 手动保存:prefix + Ctrl-s
# 手动恢复:prefix + Ctrl-r
# tmux-continuum 自动保存(每15分钟)
set -g @continuum-restore 'on' # 启动时自动恢复
set -g @continuum-save-interval '15'
远程协作(共享会话)¶
# 方式1:同一用户共享
# 用户A创建会话
tmux new -s shared
# 用户B连接到同一会话
tmux attach -t shared
# 方式2:只读观察
tmux attach -t shared -r
# 方式3:tmate(基于 tmux 的远程共享工具)
brew install tmate # 或 apt install tmate
tmate # 会生成一个共享链接
与 Neovim 深度集成¶
# 智能面板导航(Vim 和 Tmux 无缝切换)
# 需要安装 vim-tmux-navigator
# ~/.tmux.conf
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?)(diff)?$'"
bind -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'
常见问题¶
Q: 颜色显示不正确?¶
# 确保终端支持 256 色
echo $TERM # 应该是 xterm-256color
# tmux.conf 中设置
set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",xterm-256color:RGB"
Q: 复制到系统剪贴板不工作?¶
安装 tmux-yank 插件,或手动配置:
# Linux
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "xclip -sel clip"
# macOS
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "pbcopy"
Q: Tmux 和 Zellij 选哪个?¶
- Tmux:成熟稳定、插件丰富、远程服务器上必备
- Zellij:更现代的 UI、内置布局系统、新手更友好
参考资源¶
- GitHub:https://github.com/tmux/tmux
- TPM:https://github.com/tmux-plugins/tpm
- Tmux 速查表:https://tmuxcheatsheet.com/
- Catppuccin 主题:https://github.com/catppuccin/tmux
- awesome-tmux:https://github.com/rothgar/awesome-tmux