张芷铭的个人博客

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 方向键
关闭当前窗格exitCtrl-d

滚动操作

  1. Ctrl-b [ 进入复制模式
  2. 方向键逐行滚动,或 Page Up/Down 翻页
  3. g 输入行号跳转
  4. 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