张芷铭的个人博客

tmux 是终端复用器,在一个窗口运行多个会话,断开连接后会话保持运行。

安装

1
2
3
4
5
# Debian/Ubuntu
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 my_session
删除会话tmux kill-session -t my_session
删除所有tmux kill-server

窗口操作

操作快捷键
新建窗口Ctrl-b c
下一个窗口Ctrl-b n
上一个窗口Ctrl-b p
列出窗口Ctrl-b w
重命名窗口Ctrl-b ,

窗格操作

操作快捷键
垂直分割Ctrl-b %
水平分割Ctrl-b "
切换窗格Ctrl-b 方向键
关闭窗格exitCtrl-d

配置示例

1
2
3
4
# ~/.tmux.conf
set -g prefix C-a        # 前缀键改为 Ctrl-a
set -g mouse on          # 启用鼠标
bind r source-file ~/.tmux.conf \; display "Reloaded!"

会话持久化

使用 tmux-resurrect 插件保存和恢复会话:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 安装 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# ~/.tmux.conf 添加
set -g @plugin 'tmux-plugins/tmux-resurrect'
run '~/.tmux/plugins/tpm/tpm'

# 使用
# prefix + I 安装插件
# prefix + Ctrl-s 保存会话
# prefix + Ctrl-r 恢复会话

Comments