Tmux 是终端复用器,支持在一个终端窗口运行多个会话,断开连接后会话仍持续运行。
安装
1
2
3
4
5
| # Debian/Ubuntu
sudo apt update && sudo apt install tmux
# RHEL/Fedora
sudo dnf install tmux
|
启动会话
1
2
| tmux # 启动新会话
tmux new -s my_session # 启动并命名会话
|
基本操作
会话管理
| 操作 | 命令/快捷键 |
|---|
| 分离当前会话 | Ctrl-b d |
| 查看所有会话 | tmux ls |
| 重连会话 | tmux attach -t <name> |
窗口管理
| 操作 | 快捷键 |
|---|
| 创建新窗口 | Ctrl-b c |
| 下一个窗口 | Ctrl-b n |
| 上一个窗口 | Ctrl-b p |
| 重命名窗口 | Ctrl-b , |
| 列出所有窗口 | Ctrl-b w |
窗格管理
| 操作 | 快捷键 |
|---|
| 垂直分割 | Ctrl-b % |
| 水平分割 | Ctrl-b " |
| 切换窗格 | Ctrl-b 方向键 |
| 关闭当前窗格 | exit 或 Ctrl-d |
滚动操作
Ctrl-b [ 进入复制模式- 方向键逐行滚动,或
Page Up/Down 翻页 - 按
g 输入行号跳转 - 按
q 退出复制模式
配置文件
编辑 ~/.tmux.conf:
1
2
3
4
5
6
7
8
9
10
| # 设置前缀键为 Ctrl-a
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# 启用鼠标支持
set -g mouse on
# 快捷重载配置
bind r source-file ~/.tmux.conf \; display "Reloaded!"
|
重载配置:
1
| tmux source-file ~/.tmux.conf
|
会话持久化
使用 tmux-resurrect 插件保存和恢复会话:
1
2
| # 安装 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
|
在 ~/.tmux.conf 中添加:
1
2
3
| set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
run '~/.tmux/plugins/tpm/tpm'
|
重载配置后按 prefix + I 安装插件,prefix + Ctrl-s 保存会话,prefix + Ctrl-r 恢复会话。
删除会话
1
2
| tmux kill-session -t <name> # 删除指定会话
tmux kill-server # 删除所有会话
|
在会话内输入 exit 或按 Ctrl-d 可关闭当前会话。
Comments