tmux 指南

从零开始掌握 tmux。

Termio 通过网络连接到你的 tmux 窗格。本指南带你从全新安装到拥有插件驱动的精美配置——大约 15 分钟。

什么是 tmux?

tmux 是一个终端复用器——即使关闭笔记本电脑,它也能保持终端会话存活,并让你将屏幕分割为多个窗格。Termio 直接与 tmux 通信:窗格中运行的任何内容,你的 iPhone 都能实时看到。

Session(会话)

一个命名的工作空间。终端关闭后仍然存活。你可以为不同项目创建多个会话。

Window(窗口)

会话内的标签页。像浏览器标签页一样切换窗口。

Pane(窗格)

窗口内的分割区域。并排运行多个命令。Termio 连接到窗格。

每个 tmux 快捷键都以前缀键开始——默认是 Ctrl-b。按下 Ctrl-b,松开,然后按下一个键。

安装 tmux

macOS

brew install tmux

Ubuntu / Debian

sudo apt install tmux

Verify

tmux -V   # 应该输出 tmux 3.x

常用快捷键

下面推荐的配置重新映射了分割和导航键位,使其更加直观。以下快捷键假设该配置已就位。

  • Ctrl-b, then d分离——保持会话在后台运行
  • Ctrl-b, then c新建窗口(类似浏览器标签页)
  • Ctrl-b, then n / p下一个 / 上一个窗口
  • Ctrl-b, then \\水平分割窗格
  • Ctrl-b, then -垂直分割窗格
  • Ctrl-b, then h/j/k/l导航窗格(vim 风格)
  • Ctrl-b, then H/J/K/L调整当前窗格大小
  • Ctrl-b, then z缩放窗格至全屏(切换)
  • Ctrl-b, then s会话树选择器——切换会话
  • Ctrl-b, then [进入复制模式——滚动和搜索终端输出
  • Ctrl-b, then r重新加载配置(使用推荐配置时)
  • Ctrl-b, then m / M鼠标模式开 / 关(使用推荐配置时)

推荐配置

将以下内容复制到 ~/.tmux.conf。它配置了 vim 风格的窗格导航、直观的分割键位、剪贴板复制模式、tmux-power gold 主题,以及通过 resurrect + continuum 实现的会话持久化。

~/.tmux.conf

# ── General ──────────────────────────────────────────────
set -g mouse on               # click to focus, scroll, drag to resize
set -g base-index 1           # windows start at 1, not 0
setw -g pane-base-index 1
set -g renumber-windows on    # auto-renumber when a window is closed
set -g history-limit 50000    # large scrollback buffer
set -sg escape-time 10        # faster escape (vim-friendly)
set -g focus-events on

# ── Pane navigation (vim-style) ──────────────────────────
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# ── Pane resize ──────────────────────────────────────────
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

# ── Splits (stay in current directory) ───────────────────
bind '\' split-window -h -c '#{pane_current_path}'
bind  '-' split-window -v -c '#{pane_current_path}'
unbind '"'
unbind %

# ── Useful shortcuts ─────────────────────────────────────
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
bind m set -g mouse on  \; display "Mouse ON"
bind M set -g mouse off \; display "Mouse OFF"

# ── Copy mode (vi-style) ─────────────────────────────────
setw -g mode-keys vi
bind -T copy-mode-vi 'v' send -X begin-selection
bind -T copy-mode-vi 'y' send -X copy-selection

# ── Plugins (TPM) ────────────────────────────────────────
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'wfxr/tmux-power'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'tmux-plugins/tmux-yank'

# ── tmux-power theme ─────────────────────────────────────
set -g @tmux_power_theme 'gold'
set -g @tmux_power_use_bold 1
set -g @tmux_power_date_format '%m/%d'
set -g @tmux_power_time_format '%H:%M'
set -g @tmux_power_show_user 0
set -g @tmux_power_show_host 0

# ── Session persistence ──────────────────────────────────
set -g @continuum-restore 'on'
set -g @resurrect-capture-pane-contents 'on'

# Initialize TPM — keep this at the very bottom
run '~/.tmux/plugins/tpm/tpm'

安装 TPM 和插件

TPM(Tmux Plugin Manager)通过一次按键下载和管理插件。运行以下命令进行初始化:

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

然后打开新的 tmux 会话,按 Ctrl-b, Shift-I 安装配置中列出的所有插件。

推荐配置已包含以下插件:

  • tmux-plugins/tpm插件管理器——通过 prefix+I 安装其他所有插件
  • tmux-plugins/tmux-sensible大多数配置都认同的合理默认值
  • wfxr/tmux-power漂亮的 Powerline 状态栏,7 种配色主题
  • tmux-plugins/tmux-resurrect跨重启保存和恢复会话
  • tmux-plugins/tmux-continuum每 15 分钟自动保存会话
  • tmux-plugins/tmux-yank从复制模式复制到系统剪贴板

选择主题

tmux-power 提供七种配色主题。在配置中更改 @tmux_power_theme 并用 Ctrl-b r 重新加载:

  • gold
  • moon
  • coral
  • forest
  • violet
  • snow
  • sky

跨重启保持会话

启用 resurrect + continuum(推荐配置中已包含)后,你的会话和窗格内容每 15 分钟自动保存。重启后,tmux 在首次启动时恢复一切。

  • Ctrl-b, Ctrl-s手动保存会话
  • Ctrl-b, Ctrl-r手动恢复会话

快速开始清单

  1. 安装 tmux
  2. 将推荐配置复制到 ~/.tmux.conf
  3. 安装 TPM
  4. 打开新终端运行:tmux new -s work
  5. 按 Ctrl-b 然后 Shift-I 安装插件
  6. 状态栏变为金色——tmux 已就绪
  7. 在 iPhone 上打开 Termio 连接到该会话