jay's blog
tmux 快捷键

基本用法

安装

# Ubuntu 或 Debian
$ sudo apt-get install tmux

# CentOS 或 Fedora
$ sudo yum install tmux

# Mac
$ brew install tmux

启动与退出

安装完成后,键入tmux命令,就进入了 Tmux 窗口。

tmux

按下Ctrl+d或者显式输入exit命令,就可以退出 Tmux 窗口。

前缀键

Tmux 窗口有大量的快捷键。所有快捷键都要通过前缀键唤起。默认的前缀键是Ctrl+b,即先按下Ctrl+b,快捷键才会生效。

举例来说,帮助命令的快捷键是Ctrl+b ?。它的用法是,在 Tmux 窗口中,先按下Ctrl+b,再按下?,就会显示帮助信息。

然后,按下 ESC 键或q键,就可以退出帮助。

会话管理

新建会话

第一个启动的 Tmux 窗口,编号是0,第二个窗口的编号是1,以此类推。这些窗口对应的会话,就是 0 号会话、1 号会话。

使用编号区分会话,不太直观,更好的方法是为会话起名

$ tmux new -s <session-name>

分离会话

在 Tmux 窗口中,按下Ctrl+b d或者输入tmux detach命令,就会将当前会话与窗口分离。

tmux detach

上面命令执行后,就会退出当前 Tmux 窗口,但是会话和里面的进程仍然在后台运行。

tmux ls命令可以查看当前所有的 Tmux 会话。

tmux ls
# or
tmux list-session

接入会话

tmux attach命令用于重新接入某个已存在的会话。

# 使用会话编号
$ tmux attach -t 0

# 使用会话名称
$ tmux attach -t <session-name>

杀死会话

tmux kill-session命令用于杀死某个会话。

# 使用会话编号
$ tmux kill-session -t 0

# 使用会话名称
$ tmux kill-session -t <session-name>

切换会话

tmux switch命令用于切换会话。

# 使用会话编号
$ tmux switch -t 0

# 使用会话名称
$ tmux switch -t <session-name>

重命名会话

tmux rename-session命令用于重命名会话。

tmux rename-session -t 0 <new-name>

会话快捷键

Ctrl+b d:分离当前会话。
Ctrl+b s:列出所有会话。
Ctrl+b $:重命名当前会话。

最简操作流程

  • 新建会话tmux new -s my_session。
  • 在 Tmux 窗口运行所需的程序。
  • 按下快捷键Ctrl+b d将会话分离。
  • 下次使用时,重新连接到会话tmux attach-session -t my_session。

窗格操作

划分窗格

tmux split-window命令用来划分窗格。

# 划分上下两个窗格
$ tmux split-window

# 划分左右两个窗格
$ tmux split-window -h

移动光标

tmux select-pane命令用来移动光标位置。

# 光标切换到上方窗格
$ tmux select-pane -U

# 光标切换到下方窗格
$ tmux select-pane -D

# 光标切换到左边窗格
$ tmux select-pane -L

# 光标切换到右边窗格
$ tmux select-pane -R

交换窗格位置

tmux swap-pane命令用来交换窗格位置。

# 当前窗格上移
$ tmux swap-pane -U

# 当前窗格下移
$ tmux swap-pane -D

窗格快捷键

Ctrl+b %:划分左右两个窗格。
Ctrl+b ":划分上下两个窗格。
Ctrl+b <arrow key>:光标切换到其他窗格。<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:显示窗格编号。

窗口管理

新建窗口

tmux new-window命令用来创建新窗口。

$ tmux new-window

# 新建一个指定名称的窗口
$ tmux new-window -n <window-name>

切换窗口

tmux select-window命令用来切换窗口。

# 切换到指定编号的窗口
$ tmux select-window -t <window-number>

# 切换到指定名称的窗口
$ tmux select-window -t <window-name>

重命名窗口

tmux rename-window命令用于为当前窗口起名(或重命名)。

$ tmux rename-window <new-name>

窗口快捷键

Ctrl+b c:创建一个新窗口,状态栏会显示多个窗口的信息。
Ctrl+b p:切换到上一个窗口(按照状态栏上的顺序)。
Ctrl+b n:切换到下一个窗口。
Ctrl+b <number>:切换到指定编号的窗口,其中的<number>是状态栏上的窗口编号。
Ctrl+b w:从列表中选择窗口。
Ctrl+b ,:窗口重命名。

其他命令

# 列出所有快捷键,及其对应的 Tmux 命令
$ tmux list-keys

# 列出所有 Tmux 命令及其参数
$ tmux list-commands

# 列出当前所有 Tmux 会话的信息
$ tmux info

# 重新加载当前的 Tmux 配置
$ tmux source-file ~/.tmux.conf

来源:https://www.ruanyifeng.com/blog/2019/10/tmux.html

参考:https://www.cnblogs.com/zuoruining/p/11074367.html

C-b C-b     Send the prefix key                                                                                                                                                                      [19/19]
C-b C-o     Rotate through the panes
C-b C-z     Suspend the current client
C-b Space   Select next layout
C-b !       Break pane to a new window
C-b "       Split window vertically
C-b #       List all paste buffers
C-b $       Rename current session
C-b %       Split window horizontally
C-b &       Kill current window
C-b '       Prompt for window index to select
C-b (       Switch to previous client
C-b )       Switch to next client
C-b ,       Rename current window
C-b -       Delete the most recent paste buffer
C-b .       Move the current window
C-b /       Describe key binding
C-b 0       Select window 0
C-b 1       Select window 1
C-b 2       Select window 2
C-b 3       Select window 3
C-b 4       Select window 4
C-b 5       Select window 5
C-b 6       Select window 6
C-b 7       Select window 7
C-b 8       Select window 8
C-b 9       Select window 9
C-b :       Prompt for a command
C-b ;       Move to the previously active pane
C-b =       Choose a paste buffer from a list
C-b ?       List key bindings
C-b D       Choose a client from a list
C-b E       Spread panes out evenly
C-b L       Switch to the last client
C-b M       Clear the marked pane
C-b [       Enter copy mode
C-b ]       Paste the most recent paste buffer
C-b c       Create a new window
C-b d       Detach the current client
C-b f       Search for a pane
C-b i       Display window information
C-b l       Select the previously current window
C-b m       Toggle the marked pane
C-b n       Select the next window
C-b o       Select the next pane
C-b p       Select the previous pane
C-b q       Display pane numbers
C-b r       Redraw the current client
C-b s       Choose a session from a list
C-b t       Show a clock
C-b w       Choose a window from a list
C-b x       Kill the active pane
C-b z       Zoom the active pane
C-b {       Swap the active pane with the pane above
C-b }       Swap the active pane with the pane below
C-b ~       Show messages
C-b DC      Reset so the visible part of the window follows the cursor
C-b PPage   Enter copy mode and scroll up
C-b Up      Select the pane above the active pane
C-b Down    Select the pane below the active pane
C-b Left    Select the pane to the left of the active pane
C-b Right   Select the pane to the right of the active pane
C-b M-1     Set the even-horizontal layout
C-b M-2     Set the even-vertical layout
C-b M-3     Set the main-horizontal layout
C-b M-4     Set the main-vertical layout
C-b M-5     Select the tiled layout
C-b M-n     Select the next window with an alert
C-b M-o     Rotate through the panes in reverse
C-b M-p     Select the previous window with an alert
C-b M-Up    Resize the pane up by 5
C-b M-Down  Resize the pane down by 5
C-b M-Left  Resize the pane left by 5
C-b M-Right Resize the pane right by 5
C-b C-Up    Resize the pane up
C-b C-Down  Resize the pane down
C-b C-Left  Resize the pane left
C-b C-Right Resize the pane right
C-b S-Up    Move the visible part of the window up
C-b S-Down  Move the visible part of the window down
C-b S-Left  Move the visible part of the window left
C-b S-Right Move the visible part of the window right

最后修改于 2021-11-06