tmux是一种终端多路复用器:它可以在一个屏幕上创建、访问和控制多个终端。

启动tmux时,它会创建一个带有单一窗口的新会话并显示在屏幕上。 屏幕底部的状态行显示当前会话的信息,并用于输入交互式命令。

本文目前主要记录了一些tmux使用的快捷键。

持续更新中🛵🛵🛵

session

新建会话:tmux new -s <session-nmae>

查看会话:tmux ls

进入已有会话:tmux attach -t <session-name>

切换会话:tmux switch -t <session-name>

退出会话但不关闭session:tmux detach

关闭会话:exit

window

新建窗口:ctrl+b c

删除窗口:ctrl+b x

切换到下一个窗口:ctrl+b n

切换到上一个窗口:ctrl+b p

选择会话选择窗口:ctrl+b w

pane

左右分屏:ctrl+b %

上下分屏:ctrl+b "

分屏跳转:ctrl+b <方向键>

关闭当前pane:ctrl+b x

和下一窗格交换位置:ctrl+b }

和上一窗格交换位置:ctrl+b {

将当前pane转为单独的window再转换回去

将当前pane转为单独的window:ctrl+b !

window转换回pane:

  • 按 Ctrl+b 然后按 : 进入命令模式。
  • 输入 join-pane -s <源窗口编号> -t <目标窗口编号>,然后按回车。

例如,如果你想将 window 2 转换回 pane 并加入到 window 1,可以输入:

join-pane -s 2 -t 1

分屏时保持上一个pane的工作目录

~/.tmux.conf中添加下面两行:

# 设置新的pane在当前目录中打开
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"

创建新窗口时保持工作目录

# 创建新窗口时使用之前窗口所在的目录
bind c new-window -c "#{pane_current_path}"

交换窗口顺序

  1. ctrl+B :进入命令行模式。
  2. swap-window -s 1 -t 2交换窗口位置(swap-window -s <源窗口编号> -t <目标窗口编号>)。

创建新窗口插入到当前窗口之后

# Create new window after current window with Ctrl-b c
bind c new-window -a
# 与上面的配置合并之后为
bind c new-window -a -c "#{pane_current_path}"

窗口关闭时重新排序

# 窗口关闭时自动排序
set-option -g renumber-windows on

tmux.conf

setw -g mode-keys vi
set -g history-limit 99999

set-option -g base-index 1 # 窗口的初始序号:默认为0
set-option -g display-time 5000
set-option -g repeat-time 1000
set-option -g status-keys vi

set-option -g mouse off
setw -g monitor-activity off # 非当前窗口有内容更新时在状态栏通知

# panes
set-option -g pane-base-index 1
set -g pane-border-style 'fg=red'
set -g pane-active-border-style 'fg=yellow'

# 设置新的pane在当前目录中打开
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"

# 创建新窗口时使用之前窗口所在的目录
bind c new-window -a -c "#{pane_current_path}"

# 窗口关闭时自动排序
set-option -g renumber-windows on

# statusbar
set -g status-interval 1 # 状态栏刷新时间
set -g status-position bottom
set -g status-justify right
set -g status-style 'fg=red'

set -g status-left '#S'
set -g status-left-length 10

set -g status-right-style 'fg=black bg=yellow'
set -g status-right ''
set -g status-right-length 100

setw -g window-status-current-style 'fg=black bg=green'
setw -g window-status-current-format ' #I #W #F '

setw -g window-status-style 'fg=red bg=black'
setw -g window-status-format ' #I #[fg=white]#W #[fg=yellow]#F '

setw -g window-status-bell-style 'fg=yellow bg=red bold'

bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -i"