linux系统配置tmux

TMUX是一个终端复用程序,可以让用户在一个终端窗口中同时管理多个终端会话并轻松地在它们之间切换。也可以在其它窗口、会话和运行的程序之间切换,鼠标和键盘的操作都是非常方便的。在多个终端同时工作时,这可以大大提高工作效率。

tmux非常强大,愿意花时间折腾的话可以玩的花样很多。但是我一般只是用来保存和恢复工作区和防止网络不稳定掉线的情况。对我来说,tmux最大优点就是可以轻松应对无线网络不稳定的情况,运行的命令不至于因为掉线中断。

安装tmux

一般Linux不会自带tmux,需要自行安装。

如果有sudo,可以直接sudo安装,以ubuntu为例:

1
sudo apt-get install tmux

如果没有sudo,也可以用conda进行安装:

1
conda install -c conda-forge tmux

而且好像ubuntu sudo只能装2.6版本的,而conda可以安装最新版。

配置tmux

配置tmux非常简单,只需要在home目录下创建一个.tmux.conf文件并修改。

下面是我常用的配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 将快捷键前缀改成control+a
set-option -g prefix C-a
unbind-key C-a
bind-key C-a send-prefix

# 使用vi快捷键移动光标
set-window-option -g mode-keys vi

# 使用alt+方向键切换panel
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# 使用shift+方向键切换窗口
bind -n S-Left previous-window
bind -n S-Right next-window

# 启用鼠标
set -g @plugin 'tmux-plugins/tmux-yank'
set -g mouse on

# 前缀+v 垂直分割panel
bind-key v split-window -h
# 前缀+h 水平分割panel
bind-key h split-window -v

# Easy config reload
bind-key r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @resurrect-save-bash-history 'on'
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-vim 'session'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

初始化tmux, 安装conf中需要的插件

1
2
3
4
5
6
7
8
# 下载TPM代码到本地tmux插件目录中
$ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# 重新加载 Tmux 配置
$ tmux source ~/.tmux.conf

# 安装插件步骤(进入tmux之后prefix+I)
tmux

tmux常用命令

1
2
3
4
5
6
7
8
9
10
11
#启动新会话
tmux [new -s 会话名 -n 窗口名]

#恢复会话
tmux a [-t 会话名]

#列出所有会话
tmux ls

#关闭会话
tmux kill-session -t 会话名

tmux常用快捷键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 操作panel
v 垂直分割
h 水平分割
o 交换窗格
x 关闭窗格
⍽ 左边这个符号代表空格键 - 切换布局
q 显示每个窗格是第几个,当数字出现的时候按数字几就选中第几个窗格
{ 与上一个窗格交换位置
} 与下一个窗格交换位置
z 切换窗格最大化/最小化

# 操作window
c 创建新窗口
w 列出所有窗口
n 后一个窗口
p 前一个窗口
f 查找窗口
, 重命名当前窗口
& 关闭当前窗口

# 保存所有session
ctrl+s
# 恢复所有session
ctrl+r

复制

按住alt即可用鼠标选中复制,但是如果水平方向上有多个panel就会跨panel复制,需要先用prefix+z把panel最大化后再复制。


linux系统配置tmux
http://example.com/2023/05/01/linux系统配置tmux/
作者
Wang Jianhua
发布于
2023年5月1日
许可协议