Linux 窗口管理器 Tmux 使用教程

发布于 2024-05-05
1. 安装 1.1. ubuntu/debian

1. 安装

1.1. ubuntu/debian

sudo apt-get install tmux

1.2. centos/redhat/fedora

sudo yum install tmux

1.3. alpine

sudo apk add tmux

1.4. mac

brew install tmux

2. 启动和退出

2.1. 启动

tmux

执行 tmux 即可启动一个会话。

2.2. 退出

exit

3. 前缀键

tmux 的快捷键都是以 ctrl+b 为前缀的,也就是说,你需要先按下 ctrl+b 然后再按下其他键。

举例来说,在任意一个 tmux 会话中,按下 ctrl+b 然后再按下 ? 就会显示帮助文档,再按下 esc 或者 q 就可以退出帮助文档。

点击查看 tmux 帮助文档返回的快捷键
C-b C-b     Send the prefix
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 C       Customize options
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 window
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

4. 会话管理 session

4.1. 新建会话

直接输入 tmux 创建的会话会以数字命名(从 0 开始递增),如果想要给会话命名,可以使用 -s 参数。

tmux new -s <session-name>

4.2. 分离会话

分离会话就是将当前会话从终端中分离出来,这样就可以在后台运行会话。

tmux 会话中,按下 ctrl+b dtmux detach 就可以分离当前会话。

4.3. 会话列表

tmux ls

这个命令可以列出所有的会话,包含会话名称,创建时间,是否是当前所在会话。

4.4. 连接会话

使用 tmux attach 命令可以连接到一个已经存在的会话。

# 连接到最近的会话
tmux attach

# 连接到指定名称的会话
tmux attach -t <session-name>

# 也可以用缩写
tmux a -t <session-name>

4.5. 杀死会话

快捷键命令说明
ctrl+b &tmux kill-server杀死所有会话

使用 tmux kill-session 可以杀死某个会话。

# 杀死最近使用的会话
tmux kill-session

# 杀死指定名称的会话
tmux kill-session -t <session-name>

4.6. 切换会话

tmux 任意会话中才可以切换会话。

快捷键命令说明
ctrl+b stmux choose-session弹出会话列表,可以选择要切换的会话
ctrl+b (切换到上一个会话
ctrl+b )切换到下一个会话
ctrl+b L切换到最近使用的会话
tmux switch-client -t <session-name>切换到指定名称的会话

4.7. 重命名会话

使用 tmux rename-session 可以重命名会话。

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

也可以在会话中按下 ctrl+b $ 来重命名会话。

5. 窗格管理 pane

5.1. 分割窗格

tmux 会话中,可以将当前所在窗口分割成多个窗格。

快捷键命令说明
ctrl+b %tmux split-window -h水平分割
ctrl+b "tmux split-window -v垂直分割

5.2. 切换窗格/移动光标

快捷键命令说明
ctrl+b 方向键tmux select-pane -U/D/L/R切换到指定方向的窗格
ctrl+b o切换到下一个窗格
ctrl+b ;tmux last-pane切换到最近使用的窗格
ctrl+b qtmux display-panes显示窗格编号
tmux select-pane -t <pane-number>切换到指定编号的窗格

5.3. 交换窗格位置

快捷键命令说明
ctrl+b {tmux swap-pane -U与上一个窗格交换位置
ctrl+b }tmux swap-pane -D与下一个窗格交换位置
ctrl+b spacetmux select-layout <layout-name>切换窗格布局(平铺、左右、上下等)
ctrl+b ;与最近使用的窗格交换位置
ctrl+b ctrl+o所有窗格向前移动,第一个窗格会移动到最后
ctrl+b alt+o所有窗格向后移动,最后一个窗格会移动到第一个

5.4. 关闭窗格

快捷键命令说明
ctrl+b xtmux kill-paneexit关闭当前窗格
tmux kill-pane -t <pane-number>关闭指定编号的窗格
tmux kill-pane -a关闭所有窗格,将当前窗格作为默认窗格

5.5. 窗格调整大小

快捷键命令说明
ctrl+b ztmux resize-pane -Z将当前窗格全屏,再次按下恢复
ctrl+b ctrl+方向键tmux resize-pane -U/D/L/R <size>调整窗格大小

6. 窗口管理 window

6.1. 新建窗口

tmux 会话中,可以新建一个窗口,这个窗口还隶属于当前会话,但不同窗口之间是独立的,可以有不同的布局。

ctrl+b c 快捷键或命令 tmux new-window 来新建一个窗口,新建的窗口会以数字命名(从 0 开始递增),如果想要给窗口命名,可以使用 -n 参数,如 tmux new-window -n <window-name>

6.2. 重命名窗口

tmux 会话中,按下 ctrl+b , 快捷键或命令 tmux rename-window <new-name> 来重命名窗口。

6.3. 切换窗口

快捷键命令说明
ctrl+b wtmux choose-window弹出窗口列表,可以选择要切换的窗口
ctrl+b ptmux previous-window切换到上一个窗口
ctrl+b ntmux next-window切换到下一个窗口
ctrl+b ltmux last-window切换到最近使用的窗口
ctrl+b <window-number>tmux select-window -t <window-number>切换到指定编号的窗口

7. 其他命令

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

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

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

8. 状态栏

8.1. 状态栏内容

tmux 的状态栏默认是在底部的,可以显示当前窗口名称,窗口列表(编号,名称,是否是全屏模式,是否是当前窗格),如:

[ffmpeg] 0:a*Z 1:z-                "debian" 15:53 06-5月-24

其中:

  • ffmpeg 是当前窗口名称;
  • 0:a*Z 是窗口列表,0 是当前窗口编号,a 是窗口名称,* 表示当前窗口,Z 表示窗口是全屏模式;
  • 1:z- 是第二个窗口,1 是窗口编号,z 是窗口名称,- 表示窗口不是全屏模式;
  • "debian" 15:53 06-5月-24 是状态栏右侧的内容,debian 是主机名,15:53 是当前时间,06-5月-24 是当前日期。

8.2. 设置状态栏右侧内容

可以通过 set-option 命令来设置状态栏的内容。

# 设置状态栏内容
tmux set-option -g status-right "debian %H:%M %d-%b-%y"

可以使用以下变量:

  • %H 当前小时
  • %M 当前分钟
  • %d 当前日期
  • %b 当前月份
  • %y 当前年份
  • %S 当前秒钟
  • %p AM/PM
  • %I 当前小时(12小时制)
  • %k 当前小时(24小时制)
  • %l 当前分钟
  • %a 当前星期几
  • %A 当前星期几(全称)
  • %u 当前星期几(数字)
  • %U 当前年的第几周
  • %j 当前年的第几天
  • %w 当前天
  • %s 当前 Unix 时间戳

可以通过 set-option 命令来设置状态栏的颜色。

8.3. 设置状态栏颜色

# 设置状态栏颜色
tmux set-option -g status-style fg=black,bg=green

8.4. 设置状态栏位置

tmux set-option -g status-position <top|bottom>