工具大全
developer2026年3月7日53 次阅读

Tmux 终端复用器:推荐配置 + 完整使用手册

Tmux 终端复用器:推荐配置 + 完整使用手册

tmux 是一款强大的终端复用器(Terminal Multiplexer),让你在一个终端窗口中管理多个会话、窗口和面板。它最核心的能力是会话持久化——即使 SSH 断开,你的进程仍在后台运行,重新连接即可恢复工作。


为什么要用 tmux?

场景 没有 tmux 有 tmux
SSH 断开 进程全部丢失 会话持久化,重连即恢复
多任务 开多个终端窗口 一个窗口内分屏
结对编程 需要共享屏幕软件 共享 tmux 会话
服务器运维 nohup/screen 更现代、更强大的管理

安装

# macOS
brew install tmux

# Ubuntu / Debian
sudo apt install tmux

# CentOS / RHEL
sudo yum install tmux

# Arch Linux
sudo pacman -S tmux

安装后运行 tmux -V 确认版本(推荐 3.0+)。


核心概念

tmux 的层级结构:

Server(服务器)
 └── Session(会话)
      └── Window(窗口)
           └── Pane(面板)
  • Session:一个独立的工作空间,可以在后台运行
  • Window:类似浏览器的标签页
  • Pane:窗口内的分屏区域
  • Prefix Key:默认是 Ctrl+b,所有快捷键都需要先按 Prefix

推荐配置 .tmux.conf

将以下内容保存到 ~/.tmux.conf(或 ~/.config/tmux/tmux.conf):

# ============================================
# Tmux 推荐配置 (2025/2026)
# 兼容 tmux 3.0+
# ============================================

# ---------- 基础设置 ----------

# 修改 Prefix 键为 Ctrl+a(更容易按到)
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# 窗口和面板编号从 1 开始(匹配键盘布局)
set -g base-index 1
setw -g pane-base-index 1

# 关闭窗口后自动重新编号
set -g renumber-windows on

# 减少 Escape 延迟(对 Vim 用户至关重要)
set -sg escape-time 10

# 增加历史滚动缓冲区
set -g history-limit 50000

# 启用鼠标支持(点击切换面板、拖拽调整大小、滚动)
set -g mouse on

# 启用真彩色支持
set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",xterm-256color:RGB"

# 状态栏刷新间隔(秒)
set -g status-interval 5

# 焦点事件(Vim autoread 等需要)
set -g focus-events on

# ---------- 快捷键绑定 ----------

# 快速重载配置
bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"

# 更直观的分屏键(| 垂直分,- 水平分)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

# 新建窗口时保持当前路径
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

# Alt+方向键 切换面板(无需 Prefix)
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Ctrl+Shift+左/右 移动窗口
bind -n C-S-Left swap-window -t -1\; select-window -t -1
bind -n C-S-Right swap-window -t +1\; select-window -t +1

# 调整面板大小(Prefix + H/J/K/L)
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

# ---------- 复制模式(Vi 风格)----------

setw -g mode-keys vi

# v 开始选择,y 复制
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"
# Linux 用户替换为:xclip -in -selection clipboard

# ---------- 状态栏美化 ----------

# 状态栏位置
set -g status-position bottom

# 状态栏颜色
set -g status-style "bg=#1e1e2e,fg=#cdd6f4"

# 左侧:会话名
set -g status-left "#[bg=#89b4fa,fg=#1e1e2e,bold] #S #[default] "
set -g status-left-length 30

# 右侧:时间
set -g status-right "#[fg=#a6adc8] %Y-%m-%d %H:%M "
set -g status-right-length 50

# 当前窗口样式
setw -g window-status-current-format "#[bg=#cba6f7,fg=#1e1e2e,bold] #I:#W "

# 非活动窗口样式
setw -g window-status-format " #[fg=#a6adc8]#I:#W "

# 面板边框颜色
set -g pane-border-style "fg=#45475a"
set -g pane-active-border-style "fg=#89b4fa"

# 消息样式
set -g message-style "bg=#89b4fa,fg=#1e1e2e"

# ---------- 插件管理(TPM)----------

# 安装 TPM:git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# tmux-resurrect 设置
set -g @resurrect-capture-pane-contents 'on'

# tmux-continuum 设置(每 15 分钟自动保存)
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'

# 初始化 TPM(必须放在配置文件最末尾)
run '~/.tmux/plugins/tpm/tpm'

安装插件管理器 TPM

# 安装 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# 重载配置
tmux source ~/.tmux.conf

# 在 tmux 中按 Prefix + I(大写 i)安装所有插件

快捷键速查表

以下快捷键基于推荐配置(Prefix 已改为 Ctrl+a)。 使用默认配置的用户请将 Ctrl+a 替换为 Ctrl+b

会话管理

操作 命令行 快捷键
新建会话 tmux new -s name
列出会话 tmux ls Prefix + s
连接会话 tmux a -t name
断开会话 Prefix + d
重命名会话 Prefix + $
关闭会话 tmux kill-ses -t name
切换会话 Prefix + ( / )

窗口管理

操作 快捷键
新建窗口 Prefix + c
关闭窗口 Prefix + &
重命名窗口 Prefix + ,
下一个窗口 Prefix + n
上一个窗口 Prefix + p
按编号切换 Prefix + 0-9
窗口列表 Prefix + w
查找窗口 Prefix + f

面板管理

操作 快捷键
垂直分屏 Prefix + `
水平分屏 Prefix + -
切换面板(Vim 风格) Prefix + h/j/k/l
切换面板(方向键) Alt + 方向键
调整面板大小 Prefix + H/J/K/L
面板最大化/还原 Prefix + z
关闭面板 Prefix + x
显示面板编号 Prefix + q
交换面板 Prefix + { / }
切换布局 Prefix + Space

复制模式

操作 快捷键
进入复制模式 Prefix + [
开始选择 v
复制选中内容 y
退出复制模式 qEsc
粘贴 Prefix + ]

其他

操作 快捷键/命令
重载配置 Prefix + r
显示快捷键列表 Prefix + ?
进入命令模式 Prefix + :
显示时钟 Prefix + t

实战场景

场景一:远程服务器开发

# SSH 到服务器后创建工作会话
tmux new -s dev

# 分屏:左边写代码,右边跑服务
# Prefix + | 垂直分屏
# 左边 vim,右边 npm run dev

# 需要下班?断开会话
# Prefix + d

# 第二天重新连接
ssh server
tmux a -t dev   # 一切还在!

场景二:多项目工作区

# 为每个项目创建独立会话
tmux new -s frontend
tmux new -s backend
tmux new -s database

# 通过 Prefix + s 在会话间快速切换
# 通过 Prefix + ( / ) 按顺序切换

场景三:日志监控面板

tmux new -s monitor

# 创建四面板网格
# Prefix + |   (垂直分)
# Prefix + -   (水平分左)
# 切到右侧面板
# Prefix + -   (水平分右)

# 每个面板 tail 不同日志
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
tail -f /var/log/app/app.log
htop

推荐插件详解

插件 作用 说明
tmux-sensible 合理默认值 大家都认同的基础设置
tmux-yank 剪贴板集成 复制内容到系统剪贴板
tmux-resurrect 会话恢复 系统重启后恢复所有会话
tmux-continuum 自动保存 每 15 分钟自动保存会话
vim-tmux-navigator Vim 集成 Vim 和 tmux 面板无缝切换

插件安装/管理

# Prefix + I        安装新插件
# Prefix + U        更新插件
# Prefix + Alt + u  卸载未列出的插件

常见问题

Q: 怎么滚动查看历史输出?

开启鼠标模式后直接用鼠标滚轮滚动,或按 Prefix + [ 进入复制模式后用方向键/Page Up 翻页。

Q: 配色不正确怎么办?

确保终端模拟器支持 256 色或真彩色,并在 .tmux.conf 中设置了 default-terminalterminal-overrides

Q: macOS 下复制到剪贴板无效?

安装 tmux-yank 插件,或确保复制命令管道到 pbcopy。如果使用 iTerm2,勾选 "Applications in terminal may access clipboard"。

Q: 如何和 Vim/Neovim 配合使用?

推荐安装 vim-tmux-navigator 插件,实现 Ctrl+h/j/k/l 在 Vim 和 tmux 面板间无缝切换(需要同时在 Vim 中安装对应插件)。

Q: 如何完全退出 tmux?

关闭所有面板和窗口后自动退出,或运行 tmux kill-server 关闭所有会话。


参考资源

相关文章

Tmux Terminal Multiplexer: Recommended Configuration + Complete User Manual

A complete guide to the tmux terminal multiplexer for developers, including recommended .tmux.conf configuration, common shortcut key cheat sheets, plugin recommendations, and practical tips to help you significantly improve terminal efficiency.

developer2026年4月22日7 min
196

Practical Guide to Document Format Conversion: Comprehensive Analysis of Markdown, HTML, PDF Interconversion

Comprehensive analysis of conversion methods for four major document formats: Markdown, HTML, PDF, and Word, comparing the pros and cons of various conversion tools, with practical steps and solutions to common problems, helping you choose the most suitable conversion path for different scenarios.

document2026年4月22日8 min
201

Complete Guide to JWT Authentication: Principles, Usage, and Security Best Practices

JWT (JSON Web Token) is a mainstream solution for modern API authentication. This article provides an in-depth analysis of JWT's three-part structure, signature verification principles, comparison with Session, as well as key security practices such as storage location selection, expiration and refresh mechanisms, and algorithm confusion vulnerabilities.

developer2026年4月22日8 min
203

Complete Guide to Password Security: Best Practices from Creation to Management

Every year, billions of accounts are stolen due to weak passwords or password reuse. This article systematically explains common password attack methods, password strength standards, password manager selection, and the correct use of two-factor authentication, helping you fundamentally protect your digital asset security.

utility2026年4月22日7 min
206