跳转至

tmux

tmux

tmux 基础

tmux 运行命令

tmux 有两种运行命令的方式:

  • 在终端中运行完整命令,如 tmux list-session
  • 进入 tmux session 中,通过快捷键唤起命令行(prefix + :)然后输入完整命令。比如输入kill-session 关闭 session
  • 进入 tmux 后,输入各种快捷键。比如默认 prefix + ctrl-c 创建新的 sesson。各种快捷键本质也是对 tmux 命令的调用。快捷键在配置文件中通过 bind 定义。
    • 配置文件:/etc/tmux.conf, ~/.tmux.conf

session, window, panel 概念

session 表示一组进程,session 是 tmux 最大的一个粒度。session 下可以创建多个 window,windows 可以创建 panel。

通过 session 相关命令管理 session,而 window, panel 则一般通过快捷键。

列出 session,并进入

tmux ls
tmux a          #自动 attach 最近一个
tmux a -t session_name

创建 session

tmux new -s session_name
tmux                         # session name 采用数字自动编号

prefix + ctrl-c  # 快捷键

删除 session

tmux kill-session -s session_name

# 没有快捷键,推荐:`prefix :kill-session`

重命名 session

tmux rename-session -t session1 new_session_name

prefix + $   # 快捷键

默认快捷键

列出所有快捷键

tmux list-keys

组合键

组合键:默认是 ctrl-b。推荐修改组合键:

tmux set -g prefix C-x

tmux 命令行

在 tmux 窗口,输入 tmux 命令快捷键。之后就可以运行 tmux 支持的所有命令,和命令行一样

ctrl-b :

窗口管理

window

#创建
ctrl-b c #创建新的窗口
ctrl-b & #删除当前窗口

#移动
ctrl-b p #切换上一个
ctrl-b n

ctrl-b f #查找关键字

还可以通过命令

ctrl-b :kill-window

pane

# 创建
ctrl-b "      # 上下切分
ctrl-b %      # 左右切分
ctrl-b x      # 删除

# 切换pane
ctrl-b o      # 顺序遍历pane
ctrl-b 方向键  # 方向键移动

ctrl-b z      # zoom

# 移动

常用自定义配置

bind-keys 详细

bind-key [-nr] [-N note] [-T key-table] key command [arguments]

  • -T 指定绑定哪个 table
    • 默认绑定 prefix table
    • root table。不需要按 prefix 就可以触发
    • -n-T root 别名
  • -r 表示 key 可能连续输入(否则后面的会被忽略)。见 repeat-time (ms)
  • -N 添加说明,list-keys -N 可见

repeat-time 貌似只能设置全局值,而且我试了 3000ms 不起作用,仍然是默认的 500ms。

  • list-keys: List key bindings.
    • -T lists only keys in key-table.
  • send-keys key ...: Send a key or keys to a window.
    • -X is used to send a command into copy mode - see the WINDOWS AND PANES section.
  • send-prefix [-2] [-t target-pane]
    • Send the prefix key, or with -2 the secondary prefix key, to a window as if it was pressed.
  • unbind-key [-anq] [-T key-table] key
    • -a, remove all key bindings
    • The -q option prevents errors being returned.

关闭窗口不要确认

➜  ~ tmux list-keys | grep confirm-before
bind-key    -T prefix       &                     confirm-before -p "kill-window #W? (y/n)" kill-window
bind-key    -T prefix       x                     confirm-before -p "kill-pane #P? (y/n)" kill-pane

将&和 x 重新映射即可

bind-key & kill-window
bind-key x kill-pane

鼠标滚轮

windows terminal 中如何滚动鼠标

tmux set -g mouse on

swap window

prefxi:进入命令行

swap-window -s 3 -t 1     # window number 3 and window number 1 swap their positions.
swap-window -t 0          # 将当前窗口和top窗口交换
move-window -t 0          # In the unlikely case of having no window at index 0

设置快捷键

bind-key -n C-S-Left swap-window -t -1\; select-window -t -1
bind-key -n C-S-Right swap-window -t +1\; select-window -t +1

修改打开窗口时默认路径

tmux 默认路径是创建 tmux 进程时的路径,之后创建窗口和 panel 都默认是该路径。但有时我们希望能够更改默认路径。通过 attach-session 命令更改。 并且创建 panel 时,一般希望复用当前路径。可以在对应快捷键后添加-c参数即可

bind M-c attach-session -c "#{pane_current_path}" # alt-c, to change current path

# @ pane
# split current window horizontally
bind _ split-window -v -c "#{pane_current_path}"

# split current window vertically
bind - split-window -h -c "#{pane_current_path}"

tmux in tmux

tmux 内联使用方法 | 把问题都记下来 (fatfatson.github.io)

#按F12切换到内嵌tmux,在macos里需要系统设置中取消F12占用  
#1. prefix为None,不再拦截快捷键  
#2. key-table为off,下面再绑定off下的F12,使之能退出内嵌模式  
#3. 改变statusbar颜色,以便知道已进入内嵌模式  
#4. 如果处于特殊模式,退出  
unbind -T root F12  
bind -T root F12 \  
  set prefix None \;\  
  set key-table off \;\  
  set status-style bg=colour235 \;\  
  if -F '#{pane_in_mode}' 'send-keys -X cancel' \;\  
  refresh-client -S  

#在off表里绑定F12,恢复之前的设置,以退出该模式  
bind -T off F12 \  
  set -u prefix \;\  
  set -u key-table \;\  
  set -u status-style \;\  
  refresh-client -S

搜索文字

prefix+[ 进入 copy mode,然后/ 进行搜索。逻辑和 vim 一样,nshift+n 向下和向上搜索。