随笔记录
目录
一、Tmux 介绍
Tmux 就是会话与窗口的"解绑"工具,将它们彻底分离。
1)Tmux允许在多个窗口之间水平或垂直拆分窗格
2)调整窗口窗格的大小
3)会话活动监视
4)使用命令行模式编写脚本等
由于 Tmux 的这些功能十分好用,几乎所有 Unix 发行版广泛内嵌了Tmux。
类似的终端复用器还有 GNU Screen。Tmux 与它功能相似
二、Tmux 安装
Tmux 一般需要自己安装
# Ubuntu 或 Debian
$ sudo apt-get install tmux# CentOS 或 Fedora
$ sudo yum install tmux# Mac
$ brew install tmux
# 以 Centos 为例
# sudo yum install tmux
[root@bogon ~]# yum install tmux # 安装tmux
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.bupt.edu.cn
* extras: mirrors.bupt.edu.cn
* updates: mirrors.bupt.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package tmux.x86_64 0:1.8-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
tmux x86_64 1.8-4.el7 base 243 k
Transaction Summary
========================================================================================
Install 1 Package
Total download size: 243 k
Installed size: 558 k
Is this ok [y/d/N]: y # y
Downloading packages:
tmux-1.8-4.el7.x86_64.rpm | 243 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tmux-1.8-4.el7.x86_64 1/1
Verifying : tmux-1.8-4.el7.x86_64 1/1
Installed:
tmux.x86_64 0:1.8-4.el7
Complete!
[root@bogon ~]#
三、Tmux 基本使用
1. tmux 会话 启动与退出
1.1 tmux 会话启动
首先,打开终端,然后输入以下命令启动一个新的
tmux
会话
# tmux 启动会话
[root@bogon ~]#
[root@bogon ~]# tmux
# 上面命令会启动 Tmux 窗口,底部有一个状态栏。状态栏的左侧是窗口信息(编号和名称),右侧是系统信息。
1.2 tmux 会话退出
# 关闭tmux会话
1)enter 'exit' # 直接关闭tmux 会话
2) ctrl + b, than press 'x'
3)Ctrl+d
注意:1) 和 2) 和 3)区别:
1) 直接退出 Tmux 窗口
2)和 3):都是依次关闭tmux 会话中的窗口,直到关闭最后一个窗口时,退出 Tmux 窗口
1.3 前缀键
Tmux 窗口有大量的快捷键。所有快捷键都要通过前缀键唤起。默认的前缀键是
Ctrl+b
,即先按下Ctrl+b
,快捷键才会生效。举例来说,帮助命令的快捷键是
Ctrl+b ?
。它的用法是,在 Tmux 窗口中,先按下Ctrl+b
,再按下?
,就会显示帮助信息。然后,按下
ESC
键或q
键,就可以退出帮助。
2 会话管理
2.1 新建会话
启动tmux 窗口第一个编号是0,第二个窗口编号是1,以此类推。这些窗口对应的会话,就是 0
号会话、1
号会话。
使用编号区分会话,不太直观,更好的方法是为会话起名。
# 新建一个指定名称的会话
[root@bogon ~]# tmux new -s nws01
2.2 分离会话
在 Tmux 窗口中,按下
Ctrl+b d
或者输入tmux detach
命令,就会将当前会话与窗口分离。
在tmux 会话中执行
# Ctrl+b d
执行以上命令后,就会退出当前 Tmux 窗口,但是会话和里面的进程仍然在后台运行
#查看当前所有的 Tmux 会话
# tmux ls
# tmux list-session
[root@bogon ~]# tmux ls
nws01: 2 windows (created Wed Nov 22 16:33:30 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]# tmux list-session
nws01: 2 windows (created Wed Nov 22 16:33:30 2023) [108x19]
[root@bogon ~]#
# 注意:
nws01: 2 windows (created Wed Nov 22 16:33:30 2023) [108x19]
nws01: tmux 会话名字
2 windows: 会话中有2个窗口
2.3 重新接入已存在的会话
tmux attach :
用于重新接入某个已存在的会话# 使用会话编号
$ tmux attach -t 0 # 如果会话没有名字,以默认编号命名时# 使用会话名称
$ tmux attach -t <session-name>
# 重新接入已存在的 tmux 会话
# tmux attach -t <会话编号>
# tmux attach -t <会话名字>
# 注意 -t 后参数输入 tmux list-session 查询结果中 冒号前的字段即可。
若 会话有名字:nws01 , -t nws01
若 会话没有名字,已默认编号为名字, -t 默认编号即可
# 会话名是默认编号
[root@bogon ~]# tmux list-session
0: 1 windows (created Wed Nov 22 17:07:20 2023) [108x19]
1: 1 windows (created Wed Nov 22 17:07:33 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]# tmux attach -t 1 # 成功接入 已存在会话,会话名为:1
[detached]
[root@bogon ~]#
[root@bogon ~]# tmux ls
nws01: 2 windows (created Wed Nov 22 16:33:30 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# tmux list-session
nws01: 2 windows (created Wed Nov 22 16:33:30 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]# tmux attach -t nws01 # new01 : tmux 会话session 名字
2.4 杀死会话
tmux kill-session -t <Param>:
用于杀死某个会话。# 注意 -t 后参数输入 tmux list-session 查询结果中 冒号前的字段即可。
若 会话有名字:nws01 , -t nws01
若 会话没有名字,已默认编号为名字, -t 默认编号即可
1. 使用会话编号
$ tmux kill-session -t <会话编号:即 tmux list-session 查询结果中 冒号前的字段即可>
[root@bogon ~]# tmux list-session
0: 1 windows (created Wed Nov 22 17:07:20 2023) [108x19]
1: 1 windows (created Wed Nov 22 17:07:33 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]# tmux kill-session -t 1
[root@bogon ~]#
[root@bogon ~]# tmux list-session
0: 1 windows (created Wed Nov 22 17:07:20 2023) [108x19]
[root@bogon ~]#
2. 使用会话名称
$ tmux kill-session -t <session-name> # 根据会话名字杀死会话
[root@bogon ~]# tmux list-session
nws01: 2 windows (created Wed Nov 22 16:33:30 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# tmux kill-session -t nws01
[root@bogon ~]#
[root@bogon ~]# tmux list-session # 查询会话
failed to connect to server
[root@bogon ~]#
2.5 切换会话
tmux switch -t <param>:
用于切换会话# 注意 -t 后参数输入 tmux list-session 查询结果中 冒号前的字段即可。
若 会话有名字:nws01 , -t nws01
若 会话没有名字,已默认编号为名字, -t 默认编号即可
1. 使用会话编号
$ tmux switch -t 0
[root@bogon ~]# tmux list-session
0: 1 windows (created Wed Nov 22 17:07:20 2023) [108x19]
2: 1 windows (created Wed Nov 22 17:16:51 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# tmux attach -t 2 # 接入已存在会话编号为2的会话
#在编号为2的会话中 切换会话编号为0的会话
[root@bogon ~]# tmux switch -t 0
2. 使用会话名称
$ tmux switch -t <session-name>
# 创建2个会话
[root@bogon ~]# tmux new -s nws01 # 会话1:nws01
[detached]
[root@bogon ~]# tmux new -s nws02 # 会话2:nws02
[detached]
[root@bogon ~]# tmux list-session
nws01: 1 windows (created Wed Nov 22 16:53:20 2023) [108x19] # 每个会话只有1个窗口
nws02: 1 windows (created Wed Nov 22 16:53:27 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]# tmux attach -t nws02 # 接入已存在会话2: nws02
#在会话2: news02 中 切换会话1
[root@bogon ~]# tmux switch -t nws01
[root@bogon ~]#
2.6 重命名会话
tmux rename-session -t <parma1> <new_session_name>:
用于重命名会话。# 注意 -t 后参数输入 tmux list-session 查询结果中 冒号前的字段即可。
若 会话有名字:nws01 , -t nws01
若 会话没有名字,已默认编号为名字, -t 默认编号即可
#tmux rename-session -t <old_session_name or default_session_no> <new_session_name>
1. 会话没有名字,只有默认编号
[root@bogon ~]# tmux list-session # 查询已存在会话
0: 1 windows (created Wed Nov 22 17:07:20 2023) [108x19]
2: 1 windows (created Wed Nov 22 17:16:51 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]# tmux rename-session -t 2 nws12 # 将默认编号2的会话重命名 news12
[root@bogon ~]#
[root@bogon ~]# tmux list-session
0: 1 windows (created Wed Nov 22 17:07:20 2023) [108x19]
nws12: 1 windows (created Wed Nov 22 17:16:51 2023) [108x19] # 查询编号2的会话名字已更新
[root@bogon ~]#
2. 回话有名字
[root@bogon ~]# tmux list-session # 查询已存在会话 1,2
nws01: 1 windows (created Wed Nov 22 16:53:20 2023) [108x19]
nws02: 1 windows (created Wed Nov 22 16:53:27 2023) [108x19]
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# tmux rename-session -t nws01 nws11 # 将会话nws01 重命名为 new11
[root@bogon ~]# tmux list-session # 查询已存在会话 1,2
nws02: 1 windows (created Wed Nov 22 16:53:27 2023) [108x19]
nws11: 1 windows (created Wed Nov 22 16:53:20 2023) [108x19] # 会话1 名字已更新
[root@bogon ~]#
2.7 会话快捷键
一些会话快捷键
Ctrl+b d
:分离当前会话。Ctrl+b s
:列出所有会话。Ctrl+b $
:重命名当前会话。
3. 窗格操作
3.1 创建水平分屏
# 同一个窗格,水平分屏
Ctrl + b, then %
3.2 创建垂直分屏
# 同一个窗格,垂直分屏
Ctrl + b, then "
3.3 光标不同窗格切换
# 光标切换到其他窗格。是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓。
Ctrl+b <arrow key>
即:Ctrl + b, then Arrow keys
3.4 显示窗格编号
# 显示窗格编号
Ctrl + b, then 'q'
3.5 窗格组合其他键功能列表:
在
tmux
中,按下Prefix
键(默认是Ctrl + b
),然后组合其他键 实现不同功能,详细列表如下:
Ctrl+b %
:划分左右两个窗格。Ctrl+b "
:划分上下两个窗格。Ctrl+b <arrow key>
:光标切换到其他窗格。是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓。Ctrl+b ;
:光标切换到上一个窗格。Ctrl+b o
:光标切换到下一个窗格。Ctrl+b {
:当前窗格与上一个窗格交换位置。Ctrl+b }
:当前窗格与下一个窗格交换位置。Ctrl+b Ctrl+o
:所有窗格向前移动一个位置,第一个窗格变成最后一个窗格。Ctrl+b Alt+o
:所有窗格向后移动一个位置,最后一个窗格变成第一个窗格。Ctrl+b x
:关闭当前窗格。Ctrl+b !
:将当前窗格拆分为一个独立窗口。Ctrl+b z
:当前窗格全屏显示,再使用一次会变回原来大小。Ctrl+b Ctrl+<arrow key>
:按箭头方向调整窗格大小。Ctrl+b q
:显示窗格编号。
4.窗口管理
除了将一个窗口划分成多个窗格,Tmux 也允许新建多个窗口。
# tmux 启动会话
[root@bogon ~]# tmux
# 在会话窗口创建新窗口
4.1新建窗口
#在tmux的会话中,创建一个窗口
tmux new-window
-or -
Ctrl + b, then 'c'
# 新建一个指定名称的窗口
tmux new-window -n <window-name>
# 关闭窗口
enter 'exit'
4.2 切换窗口
Ctrl+b p
:切换到上一个窗口(按照状态栏上的顺序)。Ctrl+b n
:切换到下一个窗口。Ctrl+b <number>
:切换到指定编号的窗口,其中的是状态栏上的窗口编号。
Ctrl+b w
:从列表中选择窗口
# 一直向上一个窗口切换,可以通第1个切换到0,再从0切换到1
Ctrl+b p:切换到上一个窗口(按照状态栏上的顺序)。
# 一直向下一个窗口切换,可以通第0个切换到1,再从1切换到0
Ctrl+b n:切换到下一个窗口。
# 直接选择窗口编号切换
Ctrl+b <number>:切换到指定编号的窗口,其中的是状态栏上的窗口编号。
# 从列表中选择窗口
Ctrl+b, then 'w'
通过上下方向键选择 或者 直接数据 窗口号 选择切换窗口
4.3 窗口重命名
# 选中的窗口重命名
Ctrl+b , then ',' 输出重命名 -> 回车
4.4 窗口快捷键
Ctrl+b c
:创建一个新窗口,状态栏会显示多个窗口的信息。Ctrl+b p
:切换到上一个窗口(按照状态栏上的顺序)。Ctrl+b n
:切换到下一个窗口。Ctrl+b <number>
:切换到指定编号的窗口,其中的是状态栏上的窗口编号。Ctrl+b w
:从列表中选择窗口。Ctrl+b ,
:窗口重命名。
5. 其他命令
在tmux 会话中,输入一下命令
5.1 列出所有快捷键,及其对应的 Tmux 命令
1. 列出所有快捷键,及其对应的 Tmux 命令
# tmux 会话中输入一下命令
# tmux list-keys
[root@bogon ~]# tmux list-keys
bind-key ; last-pane # 注意:bind-key: Ctrl + b
bind-key = choose-buffer
bind-key ? list-keys
bind-key D choose-client
bind-key L switch-client -l
bind-key [ copy-mode
bind-key ] paste-buffer
bind-key c new-window
bind-key d detach-client
bind-key f command-prompt "find-window '%%'"
bind-key i display-message
bind-key l last-window
bind-key n next-window
bind-key o select-pane -t :.+
bind-key p previous-window
bind-key q display-panes
bind-key r refresh-client
bind-key s choose-tree
bind-key t clock-mode
bind-key w choose-window
bind-key x confirm-before -p "kill-pane #P? (y/n)" kill-pane
bind-key z resize-pane -Z
bind-key { swap-pane -U
bind-key } swap-pane -D
bind-key ~ show-messages
bind-key PPage copy-mode -u
bind-key -r Up select-pane -U
bind-key -r Down select-pane -D
bind-key -r Left select-pane -L
bind-key -r Right select-pane -R
bind-key M-1 select-layout even-horizontal
bind-key M-2 select-layout even-vertical
bind-key M-3 select-layout main-horizontal
bind-key M-4 select-layout main-vertical
bind-key M-5 select-layout tiled
bind-key M-n next-window -a
bind-key M-o rotate-window -D
bind-key M-p previous-window -a
bind-key -r M-Up resize-pane -U 5
bind-key -r M-Down resize-pane -D 5
bind-key -r M-Left resize-pane -L 5
bind-key -r M-Right resize-pane -R 5
bind-key -r C-Up resize-pane -U
bind-key -r C-Down resize-pane -D
bind-key -r C-Left resize-pane -L
bind-key -r C-Right resize-pane -R
[root@bogon ~]#
5.2 列出所有 Tmux 命令及其参数
2. 列出所有 Tmux 命令及其参数
# tmux 会话中输入一下命令
# tmux list-commands
[root@bogon ~]# tmux
lock-server (lock)
lock-session (locks) [-t target-session]
move-pane (movep) [-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]
move-window (movew) [-dkr] [-s src-window] [-t dst-window]
new-session (new) [-AdDP] [-F format] [-n window-name] [-s session-name] [-t target-session] [-x width] [-y height] [command]
new-window (neww) [-adkP] [-c start-directory] [-F format] [-n window-name] [-t target-window] [command]
next-layout (nextl) [-t target-window]
next-window (next) [-a] [-t target-session]
paste-buffer (pasteb) [-dpr] [-s separator] [-b buffer-index] [-t target-pane]
pipe-pane (pipep) [-o] [-t target-pane] [command]
previous-layout (prevl) [-t target-window]
previous-window (prev) [-a] [-t target-session]
refresh-client (refresh) [-S] [-C size][-t target-client]
rename-session (rename) [-t target-session] new-name
rename-window (renamew) [-t target-window] new-name
resize-pane (resizep) [-DLRUZ] [-x width] [-y height] [-t target-pane] [adjustment]
respawn-pane (respawnp) [-k] [-t target-pane] [command]
respawn-window (respawnw) [-k] [-t target-window] [command]
rotate-window (rotatew) [-DU] [-t target-window]
run-shell (run) [-b] [-t target-pane] shell-command
save-buffer (saveb) [-a] [-b buffer-index] path
select-layout (selectl) [-np] [-t target-window] [layout-name]
select-pane (selectp) [-lDLRU] [-t target-pane]
select-window (selectw) [-lnpT] [-t target-window]
send-keys (send) [-lR] [-t target-pane] key ...
send-prefix [-2] [-t target-pane]
server-info (info)
set-buffer (setb) [-b buffer-index] data
set-environment (setenv) [-gru] [-t target-session] name [value]
set-option (set) [-agosquw] [-t target-session|target-window] option [value]
set-window-option (setw) [-agoqu] [-t target-window] option [value]
show-buffer (showb) [-b buffer-index]
show-environment (showenv) [-g] [-t target-session] [name]
show-messages (showmsgs) [-t target-client]
show-options (show) [-gqsvw] [-t target-session|target-window] [option]
show-window-options (showw) [-gv] [-t target-window] [option]
source-file (source) path
split-window (splitw) [-dhvP] [-c start-directory] [-F format] [-p percentage|-l size] [-t target-pane] [command]
start-server (start)
suspend-client (suspendc) [-t target-client]
swap-pane (swapp) [-dDU] [-s src-pane] [-t dst-pane]
swap-window (swapw) [-d] [-s src-window] [-t dst-window]
switch-client (switchc) [-lnpr] [-c target-client] [-t target-session]
unbind-key (unbind) [-acn] [-t key-table] key
unlink-window (unlinkw) [-k] [-t target-window]
wait-for (wait) [-LSU] channel
[root@bogon ~]#
[0] 0:root@bogon:~*
5.3 列出当前所有 Tmux 会话的信息
3.列出当前所有 Tmux 会话的信息
# tmux 会话中输入一下命令
# tmux info
[root@bogon ~]# tmux
112: kNXT3: (string) \033[6;3~
113: kNXT4: (string) \033[6;4~
114: kNXT5: (string) \033[6;5~
115: kNXT6: (string) \033[6;6~
116: kNXT7: (string) \033[6;7~
117: kpp: (string) \033[5~
118: kPRV: (string) \033[5;2~
119: kPRV3: (string) \033[5;3~
120: kPRV4: (string) \033[5;4~
121: kPRV5: (string) \033[5;5~
122: kPRV6: (string) \033[5;6~
123: kPRV7: (string) \033[5;7~
124: kRIT: (string) \033[1;2C
125: kRIT3: (string) \033[1;3C
126: kRIT4: (string) \033[1;4C
127: kRIT5: (string) \033[1;5C
128: kRIT6: (string) \033[1;6C
129: kRIT7: (string) \033[1;7C
130: kUP: (string) \033[1;2A
131: kUP3: (string) \033[1;3A
132: kUP4: (string) \033[1;4A
133: kUP5: (string) \033[1;5A
134: kUP6: (string) \033[1;6A
135: kUP7: (string) \033[1;7A
136: Ms: (string) \033]52;%p1%s;%p2%s\007
137: op: (string) \033[39;49m
138: rev: (string) \033[7m
139: ri: (string) \033M
140: rmacs: (string) \033(B
141: rmcup: (string) \033[?1049l
142: rmkx: (string) \033[?1l\033>
143: setab: (string) \033[4%p1%dm
144: setaf: (string) \033[3%p1%dm
145: sgr0: (string) \033(B\033[m
146: sitm: [missing]
147: smacs: (string) \033(0
148: smcup: (string) \033[?1049h
149: smkx: (string) \033[?1h\033=
150: smso: (string) \033[7m
151: smul: (string) \033[4m
152: tsl: (string) \033]0;
153: vpa: (string) \033[%i%p1%dd
154: xenl: (flag) true
155: XT: (flag) true
Jobs:
[root@bogon ~]#
5.4补充信息
# 列出所有快捷键,及其对应的 Tmux 命令
$ tmux list-keys# 列出所有 Tmux 命令及其参数
$ tmux list-commands# 列出当前所有 Tmux 会话的信息
$ tmux info# 重新加载当前的 Tmux 配置
$ tmux source-file ~/.tmux.conf