Tmux 终端复用器使用指南

Tmux 是一个强大的终端复用器,允许在一个终端窗口中创建多个会话、窗口和面板,特别适合远程服务器管理和长时间运行的任务。
本文推荐使用 Oh My Tmux! 配置,提供强大且用户友好的 Tmux 体验。
Tmux 核心概念
Section titled “Tmux 核心概念”- Session(会话):一组窗口的集合,可以保存和恢复
- Window(窗口):一个全屏视图,包含多个面板
- Pane(面板):窗口中的一个分割区域
安装 Tmux
Section titled “安装 Tmux”Linux / macOS
Section titled “Linux / macOS”安装 Oh My Tmux 配置:
cdgit clone https://github.com/gpakosz/.tmux.gitln -s -f .tmux/.tmux.confcp .tmux/.tmux.conf.local .Windows (WSL)
Section titled “Windows (WSL)”Tmux 运行在 Linux 上,Windows 需要使用 WSL(Windows Subsystem for Linux)。
-
安装 WSL(如果未安装):
以管理员身份打开 PowerShell 并运行:
Terminal window wsl --install这将默认安装 Ubuntu。如果提示,请重启计算机。
-
在 WSL 中安装 Tmux:
打开 WSL 终端并运行:
Terminal window sudo apt updatesudo apt install tmux -
安装 Oh My Tmux(可选但推荐):
Terminal window cdgit clone https://github.com/gpakosz/.tmux.gitln -s -f .tmux/.tmux.confcp .tmux/.tmux.conf.local .
Linux / macOS
Section titled “Linux / macOS”To install this configuration:
cdgit clone https://github.com/gpakosz/.tmux.gitln -s -f .tmux/.tmux.confcp .tmux/.tmux.conf.local .Windows (WSL)
Section titled “Windows (WSL)”Tmux runs natively on Linux, so on Windows you need to use WSL (Windows Subsystem for Linux).
-
Install WSL (if not already installed):
Open PowerShell as Administrator and run:
Terminal window wsl --installThis installs Ubuntu by default. Restart your computer if prompted.
-
Install Tmux inside WSL:
Open the WSL terminal and run:
Terminal window sudo apt updatesudo apt install tmux -
Install Oh My Tmux (optional but recommended):
Terminal window cdgit clone https://github.com/gpakosz/.tmux.gitln -s -f .tmux/.tmux.confcp .tmux/.tmux.conf.local .
配置 Tmux
Section titled “配置 Tmux”配置文件分为两个文件:
~/.tmux.conf:主配置文件(请勿直接编辑)~/.tmux.conf.local:本地自定义配置(在此文件中编辑)
推荐自定义配置
Section titled “推荐自定义配置”在 ~/.tmux.conf.local 中添加以下配置以提高可用性:
# 增加历史记录大小set -g history-limit 50000
# 将前缀键更改为反引号 (`)unbind C-bset -g prefix `bind ` send-prefix
# 将状态栏移到顶部set -g status-position top
# 启用鼠标模式(可选)set -g mouse on
# 启用 vi 模式进行复制set -g mode-keys vi
# 设置默认终端颜色set -g default-terminal "screen-256color"以下是一些常用快捷键(假设使用默认前缀 C-b 或自定义前缀):
| 快捷键 | 功能 |
|---|---|
Prefix + c | 创建新窗口 |
Prefix + n | 下一个窗口 |
Prefix + p | 上一个窗口 |
Prefix + 0-9 | 切换到指定窗口 |
Prefix + , | 重命名当前窗口 |
| 快捷键 | 功能 |
|---|---|
Prefix + % | 垂直分割窗口 |
Prefix + " | 水平分割窗口 |
Prefix + →/←/↑/↓ | 在面板间导航 |
Prefix + z | 切换面板全屏 |
Prefix + x | 关闭当前面板 |
Prefix + Space | 循环切换面板布局 |
| 快捷键 | 功能 |
|---|---|
Prefix + d | 分离当前会话 |
Prefix + s | 显示会话列表 |
Prefix + $ | 重命名当前会话 |
Prefix + ( / ) | 切换上一个/下一个会话 |
其他常用快捷键
Section titled “其他常用快捷键”| 快捷键 | 功能 |
|---|---|
Prefix + r | 重新加载配置文件 |
Prefix + ? | 显示快捷键帮助 |
Prefix + [ | 进入复制模式 |
Prefix + ] | 粘贴剪贴板内容 |
Prefix + : | 进入命令模式 |
1. 保存和恢复会话
Section titled “1. 保存和恢复会话”使用插件 tmux-resurrect 可以保存和恢复会话:
# 安装插件(在 .tmux.conf.local 中添加)set -g @plugin 'tmux-plugins/tmux-resurrect'set -g @plugin 'tmux-plugins/tmux-continuum'
# 保存会话:Prefix + Ctrl+s# 恢复会话:Prefix + Ctrl+r2. 系统剪贴板集成
Section titled “2. 系统剪贴板集成”配置系统剪贴板集成:
# Linux (需要 xclip)bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
# macOS (需要 reattach-to-user-namespace)set -g @plugin 'tmux-plugins/tmux-yank'3. 面板同步
Section titled “3. 面板同步”在多个面板中同步输入:
# 启用/禁用面板同步Prefix + :set synchronize-panes4. 远程服务器管理
Section titled “4. 远程服务器管理”Tmux 特别适合远程服务器管理:
# 在服务器上启动 Tmuxtmux new -s mysession
# 分离会话(保持运行)Prefix + d
# 重新连接会话tmux attach -t mysession1. 鼠标滚动不工作
Section titled “1. 鼠标滚动不工作”确保鼠标模式已启用:
set -g mouse on2. 颜色显示不正确
Section titled “2. 颜色显示不正确”设置正确的终端类型:
set -g default-terminal "screen-256color"3. 复制粘贴不工作
Section titled “3. 复制粘贴不工作”确保安装了必要的插件并配置了剪贴板集成。