Tmux Terminal Multiplexer: Recommended Configuration + Complete User Manual
Tmux Terminal Multiplexer: Recommended Configuration + Complete User Manual
tmux is a powerful terminal multiplexer that allows you to manage multiple sessions, windows, and panes in a single terminal window. Its core capability is session persistence—even if SSH disconnects, your processes continue running in the background, and you can resume work upon reconnecting.
Why Use tmux?
| Scenario | Without tmux | With tmux |
|---|---|---|
| SSH disconnect | All processes lost | Session persists, resume upon reconnect |
| Multitasking | Open multiple terminal windows | Split screen within one window |
| Pair programming | Requires screen sharing software | Share tmux session |
| Server operations | nohup/screen | More modern and powerful management |
Installation
# macOS
brew install tmux
# Ubuntu / Debian
sudo apt install tmux
# CentOS / RHEL
sudo yum install tmux
# Arch Linux
sudo pacman -S tmux
After installation, run tmux -V to confirm the version (recommended 3.0+).
Core Concepts
The hierarchical structure of tmux:
Server(服务器)
└── Session(会话)
└── Window(窗口)
└── Pane(面板)
- Session: An independent workspace that can run in the background
- Window: Similar to browser tabs
- Pane: Split screen areas within a window
- Prefix Key: Default is
Ctrl+b, all shortcuts require pressing Prefix first
Recommended Configuration .tmux.conf
Save the following content to ~/.tmux.conf (or ~/.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'
Install Plugin Manager TPM
# 安装 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# 重载配置
tmux source ~/.tmux.conf
# 在 tmux 中按 Prefix + I(大写 i)安装所有插件
Shortcut Key Cheat Sheet
The following shortcuts are based on the recommended configuration (Prefix changed to
Ctrl+a). Users with default configuration should replaceCtrl+awithCtrl+b.
Session Management
| Action | Command Line | Shortcut |
|---|---|---|
| New session | tmux new -s name |
|
| List sessions | tmux ls |
Prefix + s |
| Attach session | tmux a -t name |
|
| Detach session | Prefix + d |
|
| Rename session | Prefix + $ |
|
| Kill session | tmux kill-ses -t name |
|
| Switch session | Prefix + ( / ) |
Window Management
| Action | Shortcut |
|---|---|
| New window | Prefix + c |
| Close window | Prefix + & |
| Rename window | Prefix + , |
| Next window | Prefix + n |
| Previous window | Prefix + p |
| Switch by number | Prefix + 0-9 |
| Window list | Prefix + w |
| Find window | Prefix + f |
Pane Management
| Action | Shortcut |
|---|---|
| Vertical split | Prefix + ` |
| Horizontal split | Prefix + - |
| Switch pane (Vim style) | Prefix + h/j/k/l |
| Switch pane (arrow keys) | Alt + arrow keys |
| Resize pane | Prefix + H/J/K/L |
| Maximize/restore pane | Prefix + z |
| Close pane | Prefix + x |
| Show pane number | Prefix + q |
| Swap panes | Prefix + { / } |
| Change layout | Prefix + Space |
Copy Mode
| Action | Shortcut |
|---|---|
| Enter copy mode | Prefix + [ |
| Start selection | v |
| Copy selection | y |
| Exit copy mode | q or Esc |
| Paste | Prefix + ] |
Others
| Action | Shortcut/Command |
|---|---|
| Reload config | Prefix + r |
| Show shortcut list | Prefix + ? |
| Enter command mode | Prefix + : |
| Show clock | Prefix + t |
Practical Scenarios
Scenario 1: Remote Server Development
# SSH 到服务器后创建工作会话
tmux new -s dev
# 分屏:左边写代码,右边跑服务
# Prefix + | 垂直分屏
# 左边 vim,右边 npm run dev
# 需要下班?断开会话
# Prefix + d
# 第二天重新连接
ssh server
tmux a -t dev # 一切还在!
Scenario 2: Multi-Project Workspace
# 为每个项目创建独立会话
tmux new -s frontend
tmux new -s backend
tmux new -s database
# 通过 Prefix + s 在会话间快速切换
# 通过 Prefix + ( / ) 按顺序切换
Scenario 3: Log Monitoring Panel
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
Detailed Plugin Recommendations
| Plugin | Function | Description |
|---|---|---|
| tmux-sensible | Sensible defaults | Basic settings agreed upon by everyone |
| tmux-yank | Clipboard integration | Copy content to system clipboard |
| tmux-resurrect | Session restore | Restore all sessions after system reboot |
| tmux-continuum | Auto-save | Automatically save sessions every 15 minutes |
| vim-tmux-navigator | Vim integration | Seamless switching between Vim and tmux panes |
Plugin Installation/Management
# Prefix + I 安装新插件
# Prefix + U 更新插件
# Prefix + Alt + u 卸载未列出的插件
Frequently Asked Questions
Q: How to scroll and view historical output?
After enabling mouse mode, use the mouse wheel to scroll directly, or press Prefix + [ to enter copy mode and use arrow keys/Page Up to page through.
Q: What to do if the color scheme is incorrect?
Ensure the terminal emulator supports 256 colors or true color, and set default-terminal and terminal-overrides in .tmux.conf.
Q: Copy to clipboard doesn't work on macOS?
Install the tmux-yank plugin, or ensure the copy command is piped to pbcopy. If using iTerm2, check "Applications in terminal may access clipboard".
Q: How to use with Vim/Neovim?
It is recommended to install the vim-tmux-navigator plugin to achieve seamless switching between Vim and tmux panes with Ctrl+h/j/k/l (requires installing the corresponding plugin in Vim as well).
Q: How to completely exit tmux?
It will exit automatically after closing all panes and windows, or run tmux kill-server to close all sessions.
