后台 shell 常驻工具
Reference: tmux wiki
Fully tutorial
为什么使用 tmux
在我们日常使用 shell 运行命令的时候可能我们是使用 ssh 链接的服务器。而当 ssh 断开的时候终端也会随之中断。在 ubuntu 系统中我们可以使用 screen 来让 shell 命令挂在后台运行。而 tmux 作为 screen 的上位替代则比 screen 带来了更多的功能。
Feature
tmux 的结构逻辑跟linux系统很像. 如果你对linux系统熟悉的话可以简单将tmux的session理解为用户. windows 就是用户桌面环境. 分屏功能就像是你在一个window中可以同时打开多个软件.
Installation | 安装 tmux
# ubuntu
sudo apt install tmux
# macos
brew install tmux
quick start
- 启动 tmux
- 不带参数执行此命令会自动创建一个新的 session(在用户不主动关闭的情况在 session 即使在 tmux 关闭后仍然会继续运行)
tmux
- 分屏:
- 左右分屏: “ctrlb + % "
- 上下分屏: “ctrlb + " "
- 切换焦点(当你需要切换同一个 session 中不同的窗口时)
- “ctrl” + " b + “<方向键>”
-
创建新的 window: “ctrlb + c”
-
切换 window: “ctrlb +
" | 切换到下一个window: “ctrlb + n”
一个 session 可以包括多个 windows
-
退出 tmux: “ctrlb” + “d”
-
列出 session
tmux ls
- 恢复 tmux:
tmux a
- 删除 session:
tmux kill-session -t <session_name>
Advance setting
session
# 创建制定名称的session
tmux new -s <session_name>
# 回复到指定的session
tmux a -t <session_name>
# 窗口分屏快速切换聚焦(摁下q的时候会看到分屏序号)
# "ctrlb + q + <index>"
# tmux 概览(你会看到当前系统中所有的sesion和窗口,通过方向键就能快速预览窗口,enter就能直接切换到聚焦的window)
# “ctrlb + w”
tmux 配置文件 (解锁鼠标控制
- 进入配置文件
nano ~/.tmux.conf
-
开启鼠标调整窗口大小的支持: “set -g mouse on”
-
更改快捷键: “set -g prefix C-s”(意思是将原本用 ctrl+b 的快捷键改为用 ctrl+s)
Tmux 插件管理器 (TPM)
什么是 TPM
TPM (Tmux Plugin Manager) 是 tmux 的官方插件管理器,它可以让你轻松安装、更新和管理 tmux 插件。
仓库地址: https://github.com/tmux-plugins/tpm
安装 TPM
# 克隆 TPM 到本地插件目录
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
配置 TPM
在 ~/.tmux.conf 文件底部添加以下内容:
# 插件列表
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
# 初始化 TPM(必须放在配置文件的最后一行)
run '~/.tmux/plugins/tpm/tpm'
重新加载 tmux 配置:
# 在终端执行(如果 tmux 正在运行)
tmux source ~/.tmux.conf
TPM 使用快捷键
按 prefix(默认 Ctrl+b)+ 以下按键:
I(大写 i) - 安装新插件U(大写 u) - 更新所有插件alt+u- 卸载未列出的插件
推荐插件
1. tmux-yank - 系统剪切板集成
将 tmux 内容复制到系统剪切板,或从系统剪切板粘贴到 tmux。
安装
在 ~/.tmux.conf 的插件列表中添加:
set -g @plugin 'tmux-plugins/tmux-yank'
按 prefix + I 安装插件。
功能
- 复制模式:选中文本后自动复制到系统剪切板
- 快捷键:
prefix + y- 复制命令行到剪切板prefix + Y(Shift+y) - 复制当前工作目录
系统依赖
macOS:
brew install reattach-to-user-namespace
在 ~/.tmux.conf 添加:
set -g default-command "reattach-to-user-namespace -l $SHELL"
Linux:
# Ubuntu/Debian
sudo apt install xclip
# Fedora
sudo dnf install xclip
# Arch
sudo pacman -S xclip
2. tmux-resurrect - 保存和恢复会话
无需配置即可保存 tmux 环境的所有细节,并在系统重启后恢复。
仓库: tmux-plugins/tmux-resurrect
安装
set -g @plugin 'tmux-plugins/tmux-resurrect'
功能
- 完全恢复 tmux 环境:sessions、windows、panes、pane布局
- 恢复程序状态:vim、man、less、htop 等工具的运行状态
- 恢复工作目录和光标位置
快捷键
prefix + Ctrl-s- 保存当前 tmux 环境prefix + Ctrl-r- 恢复保存的环境
高级配置(可选)
# 启用 vim 会话恢复
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
# 恢复更多程序的状态
for program in 'fish' 'ksh' 'zsh'; do
set -g @resurrect-strategy-$program 'session'
done
3. tmux-continuum - 自动保存会话
tmux-resurrect 的增强版,实现自动保存和恢复 tmux 环境。
仓库: tmux-plugins/tmux-continuum
安装
set -g @plugin 'tmux-plugins/continuum'
配置
在 ~/.tmux.conf 添加:
# 每 15 分钟自动保存一次
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
功能
- 按时间间隔自动保存 tmux 环境
- tmux 启动时自动恢复上次保存的环境
- 可选在启动时静默恢复
快捷键
prefix + Ctrl-s- 手动触发即时保存
完整配置示例
以下是一个包含 TPM 和推荐插件的完整 ~/.tmux.conf 配置:
# ============================================
# 基础配置
# ============================================
# 更改前缀键为 Ctrl+s
set -g prefix C-s
unbind C-b
bind C-s send-prefix
# 开启鼠标支持
set -g mouse on
# 设置窗口和面板索引从 1 开始
set -g base-index 1
setw -g pane-base-index 1
# 重新加载配置文件
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
# ============================================
# 插件列表
# ============================================
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/continuum'
# ============================================
# 插件配置
# ============================================
# tmux-resurrect 配置
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-vim 'session'
# tmux-continuum 配置
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
# ============================================
# 初始化 TPM(必须放在最后一行)
# ============================================
run '~/.tmux/plugins/tpm/tpm'
安装插件步骤:
- 保存配置文件
- 按
Ctrl+s + I(大写 i) 安装所有插件 - 重启 tmux 或执行
tmux source ~/.tmux.conf