tmux

tmux 采用 client/server模型,主要有四个模块:
- server:服务。tmux运行的基础服务,以下模块均依赖于此服务;
- session:会话。一个服务可以包含多个会话;
- window:窗口。一个会话可以包含多个窗口;
- panel:窗格/面板。一个窗口可以包含多个窗格
会话相关命令:https://zhuanlan.zhihu.com/p/137715607
2.1、会话的快捷键
- s:列出所有会话
- d:离开当前会话
- $:重命名当前会话
- 查看已有会话:tmux ls
- 新建会话:tmux new -s <session-name>
- 接入会话:tmux attach -t <session-name> 或 tmux a -t 0
- 重命名会话:tmux rename-session -t 0 <new-name>
- 切换会话:tmux switch -t <session-name> 或 tmux s -t 0
- 杀死会话:tmux kill-session -t <session-name> 或 tmux kill-session -t 0
窗口的快捷键
- c:创建一个新窗口
- n:切换到下一个窗口
- w:从列表中选择窗口
- <0~9>:切换到指定编号的窗口,编号显示在状态栏
- ,:窗口重命名
2.3、窗格的快捷键

- %:分成左右两个窗格
- “:分成上下两个窗格
- z:当前窗格全屏显示,再按一次恢复
- q:显示窗格编号
- t:在当前窗格显示时间
- <arrow key>:光标切换到其他窗格
- o:光标切换到下一个窗格
- {:左移当前窗格
- }:右移当前窗格
- Ctrl+o:上移当前窗格
- Alt+o:下移当前窗格
- space:切换窗格布局
Tmux 是一个强大的终端复用器,它允许用户在一个窗口中访问多个会话,并支持会话与窗口的分离与重连。这里提供了一些常用的 Tmux 快捷键,以便快速参考和使用。
会话(Session)操作
会话操作
新建会话: tmux new -s <session-name>
分离会话: Ctrl+b d
断开会话: Ctrl+b d
列出所有会话: Ctrl+b s
接入会话: tmux attach -t <session-name>
接入会话: tmux attach -t <会话名称>
杀死会话: tmux kill-session -t <session-name>
结束会话: tmux kill-session -t <会话名称>
切换会话: tmux switch -t <session-name>
切换会话: tmux switch -t <会话名称>
重命名会话: Ctrl+b $
窗口(Window)操作
创建新窗口: Ctrl+b c
切换到上一个窗口: Ctrl+b p
切换到下一个窗口: Ctrl+b n
切换到指定编号的窗口: Ctrl+b 0..9
重命名当前窗口: Ctrl+b ,
关闭当前窗口: Ctrl+b &
窗格(Pane)操作
垂直分割窗格: Ctrl+b %
水平分割窗格: Ctrl+b "
交换窗格位置: Ctrl+b o
关闭当前窗格: Ctrl+b x
显示窗格编号: Ctrl+b q
文本模式操作
在文本模式下,可以使用以下快捷键进行文本搜索和复制:
进入文本复制模式: PREFIX-[
搜索文本: / 后输入搜索内容
复制文本: 使用 vi 的选中、复制命令
杂项
其他一些有用的快捷键包括:
显示数字时钟: Ctrl+b t
列出所有快捷键: Ctrl+b ?
进入命令行模式: Ctrl+b :
记住,使用 Tmux 快捷键时,先按下前缀键(默认为Ctrl+b),然后释放并按下相应的快捷键。例如,要创建新窗口,先按Ctrl+b,然后按c键。
Tmux 的这些快捷键可以大大提高终端操作的效率,是开发人员的得力助手。
配置示例:
# remap prefix from 'C-b' to 'C-\'
set-option -g prefix 'C-\'
unbind-key C-b
bind-key 'C-\' send-prefix
# disable the repeat
set-option -g repeat-time 0
# start window numbers at 1 to match keyboard order with tmux window order
set-option -g base-index 1
set-window-option -g pane-base-index 1
# renumber windows sequentially after closing any of them
set-option -g renumber-windows off
# auto window rename
set-option -g automatic-rename on
# enable activity alerts
set-window-option -g monitor-activity off
set-option -g visual-activity off
# increase scrollback lines
set-option -g history-limit 50000
# enable clipboard
set-option -g set-clipboard on
# mouse behavior
set-option -g mouse on
# disable escape delay
set-option -sg escape-time 0
# truecolor support
set-option -ga terminal-overrides ",xterm-256color:Tc"
# default terminal is 256 colors
set-option -g default-terminal "xterm-256color"
# aggressive resize
set-window-option -g aggressive-resize off
# iTerm2 window name
set-option -g set-titles on
# moving between panes.
bind-key -r h select-pane -L
bind-key -r j select-pane -D
bind-key -r k select-pane -U
bind-key -r l select-pane -R
# resize panes with vim movement keys
bind-key -r H resize-pane -L 5
bind-key -r J resize-pane -D 5
bind-key -r K resize-pane -U 5
bind-key -r L resize-pane -R 5
# remove confirmation questions when closing windows/panes
bind-key x kill-pane
bind-key & kill-window
# go to last window
bind-key ^ last-window
# splitting panes with current path
bind-key c new-window -c "#{pane_current_path}"
bind-key % split-window -h -c "#{pane_current_path}"
bind-key '"' split-window -v -c "#{pane_current_path}"
setw -g mode-style fg=#e9eef9,bg=#312D45
# move panes to another window
bind-key M-1 join-pane -t :1
bind-key M-2 join-pane -t :2
bind-key M-3 join-pane -t :3
bind-key M-4 join-pane -t :4
bind-key M-5 join-pane -t :5
bind-key M-6 join-pane -t :6
bind-key M-7 join-pane -t :7
bind-key M-8 join-pane -t :8
bind-key M-9 join-pane -t :9
# Use vim keybindings in copy mode
# set-window-option -g mode-keys vi
# Update default binding of `Enter` and `Space to also use copy-pipe
unbind-key -T copy-mode-vi Enter
unbind-key -T copy-mode-vi Space
bind-key -T edit-mode-vi Up send-keys -X history-up
bind-key -T edit-mode-vi Down send-keys -X history-down
# begin selection as in Vim
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send -X rectangle-toggle
# copy text in copy mode
bind-key -T copy-mode-vi y send -X copy-selection-and-cancel
# popup window for finding command to run
bind-key R display-popup -E "tmux-commands | xargs -I{} tmux send-keys '{}' enter"
bind-key C display-popup -E "tmux-commands | xargs tmux new-window -c '#{pane_current_path}'"
bind-key | display-popup -E "tmux-commands | xargs tmux split-window -v -c '#{pane_current_path}'"
bind-key - display-popup -E "tmux-commands | xargs tmux split-window -h -c '#{pane_current_path}'"
# popup window for finding session to switch
bind-key S display-popup -E "tmux-sessions | xargs tmux switch-client -t"
bind-key P if-shell -F '#{==:#{session_name},popup}' {
detach-client
} {
if-shell -F '#{e|==:#{N/s:popup},0}' {
new-session -d -s 'popup' 'tmux source-file ~/.dotfiles/tmux/sessions/popup.tmux.conf'
}
display-popup -w 80% -h 80% -E "tmux attach-session -t popup"
}
# shortcut for synchronize-panes toggle
bind-key I set-window-option synchronize-panes
# set fish as default shell if available
# install by linux package manager
if-shell 'test -e /usr/bin/fish' {
set-option -g default-shell /usr/bin/fish
}
# install by homebrew
if-shell 'test -e /usr/local/bin/fish' {
set-option -g default-shell /usr/local/bin/fish
}
# install by macports
if-shell 'test -e /opt/local/bin/fish' {
set-option -g default-shell /opt/local/bin/fish
}
# status bar
if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"
set-hook -g window-linked 'if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"'
set-hook -g window-unlinked 'if -F "#{==:#{session_windows},1}" "set -g status off" "set -g status on"'
set-option -g status-interval 1
set-option -g status-position bottom
set-option -g status-justify left
set-option -g status-style none
# status bar left right hidden
set-option -g status-left ""
set-option -g status-right ""
# window bar style
# set-option -g status off
set-option -g window-status-style "fg=#16151B,bg=#16151B"
set-option -g window-status-current-style "fg=#edecee,bg=#49556a"
set-option -g window-status-activity-style "fg=#edecee,bg=#16151B"
set-option -g window-status-format '#[fg=#edecee] #I '
set-option -g window-status-current-format '#[fg=#edecee] #I '
set-option -g window-status-separator ''
unbind-key -T copy-mode-vi MouseDragEnd1Pane
# local config
if-shell 'test -e ~/.tmux.conf.local' {
source-file ~/.tmux.conf.local
}
Ctrl+b 激活控制台;此时以下按键生效
系统操作 ? 列出所有快捷键;按q返回
d 脱离当前会话;这样可以暂时返回Shell界面,输入tmux attach能够重新进入之前的会话
D 选择要脱离的会话;在同时开启了多个会话时使用
Ctrl+z 挂起当前会话
r 强制重绘未脱离的会话
s 选择并切换会话;在同时开启了多个会话时使用
: 进入命令行模式;此时可以输入支持的命令,例如kill-server可以关闭服务器
[ 进入复制模式;此时的操作与vi/emacs相同,按q/Esc退出
~ 列出提示信息缓存;其中包含了之前tmux返回的各种提示信息
窗口操作 c 创建新窗口
& 关闭当前窗口
数字键 切换至指定窗口
p 切换至上一窗口
n 切换至下一窗口
l 在前后两个窗口间互相切换
w 通过窗口列表切换窗口
, 重命名当前窗口;这样便于识别
. 修改当前窗口编号;相当于窗口重新排序
f 在所有窗口中查找指定文本
面板操作 ” 将当前面板平分为上下两块
% 将当前面板平分为左右两块
x 关闭当前面板
! 将当前面板置于新窗口;即新建一个窗口,其中仅包含当前面板
Ctrl+方向键 以1个单元格为单位移动边缘以调整当前面板大小
Alt+方向键 以5个单元格为单位移动边缘以调整当前面板大小
Space 在预置的面板布局中循环切换;依次包括even-horizontal、even-vertical、main-horizontal、main-vertical、tiled
q 显示面板编号
o 在当前窗口中选择下一面板
方向键 移动光标以选择面板
{ 向前置换当前面板
} 向后置换当前面板
Alt+o 逆时针旋转当前窗口的面板
Ctrl+o 顺时针旋转当前窗口的面板
Tmux 快捷键 & 速查表
启动新会话:
tmux [new -s 会话名 -n 窗口名]
恢复会话:
tmux at [-t 会话名]
列出所有会话:
tmux ls
关闭会话:
tmux kill-session -t 会话名
关闭所有会话:
tmux ls | grep : | cut -d. -f1 | awk ‘{print substr($1, 0, length($1)-1)}’ | xargs kill
在 Tmux 中,按下 Tmux 前缀 ctrl+b,然后:
会话
:new<回车> 启动新会话
s 列出所有会话
$ 重命名当前会话
窗口 (标签页)
c 创建新窗口
w 列出所有窗口
n 后一个窗口
p 前一个窗口
f 查找窗口
, 重命名当前窗口
& 关闭当前窗口
调整窗口排序
swap-window -s 3 -t 1 交换 3 号和 1 号窗口
swap-window -t 1 交换当前和 1 号窗口
move-window -t 1 移动当前窗口到 1 号
窗格(分割窗口)
% 垂直分割
” ” 水平分割
o 交换窗格
x 关闭窗格
⍽ 左边这个符号代表空格键 – 切换布局
q 显示每个窗格是第几个,当数字出现的时候按数字几就选中第几个窗格
{ 与上一个窗格交换位置
} 与下一个窗格交换位置
z 切换窗格最大化/最小化
同步窗格
这么做可以切换到想要的窗口,输入 Tmux 前缀和一个冒号呼出命令提示行,然后输入:
:setw synchronize-panes
你可以指定开或关,否则重复执行命令会在两者间切换。 这个选项值针对某个窗口有效,不会影响别的会话和窗口。 完事儿之后再次执行命令来关闭。帮助
调整窗格尺寸
如果你不喜欢默认布局,可以重调窗格的尺寸。虽然这很容易实现,但一般不需要这么干。这几个命令用来调整窗格:
PREFIX : resize-pane -D 当前窗格向下扩大 1 格
PREFIX : resize-pane -U 当前窗格向上扩大 1 格
PREFIX : resize-pane -L 当前窗格向左扩大 1 格
PREFIX : resize-pane -R 当前窗格向右扩大 1 格
PREFIX : resize-pane -D 20 当前窗格向下扩大 20 格
PREFIX : resize-pane -t 2 -L 20 编号为 2 的窗格向左扩大 20 格
文本复制模式:
按下**前缀 [**进入文本复制模式。可以使用方向键在屏幕中移动光标。默认情况下,方向键是启用的。在配置文件中启用 Vim 键盘布局来切换窗口、调整窗格大小。Tmux 也支持 Vi 模式。要是想启用 Vi 模式,只需要把下面这一行添加到 .tmux.conf 中:
setw -g mode-keys vi
启用这条配置后,就可以使用 h、j、k、l 来移动光标了。
想要退出文本复制模式的话,按下回车键就可以了。一次移动一格效率低下,在 Vi 模式启用的情况下,可以辅助一些别的快捷键高效工作。
例如,可以使用 w 键逐词移动,使用 b 键逐词回退。使用 f 键加上任意字符跳转到当前行第一次出现该字符的位置,使用 F 键达到相反的效果。
vi emacs 功能
^ M-m 反缩进
Escape C-g 清除选定内容
Enter M-w 复制选定内容
j Down 光标下移
h Left 光标左移
l Right 光标右移
L 光标移到尾行
M M-r 光标移到中间行
H M-R 光标移到首行
k Up 光标上移
d C-u 删除整行
D C-k 删除到行末
$ C-e 移到行尾
: g 前往指定行
C-d M-Down 向下滚动半屏
C-u M-Up 向上滚动半屏
C-f Page down 下一页
w M-f 下一个词
p C-y 粘贴
C-b Page up 上一页
b M-b 上一个词
q Escape 退出
C-Down or J C-Down 向下翻
C-Up or K C-Up 向下翻
n n 继续搜索
? C-r 向前搜索
/ C-s 向后搜索
0 C-a 移到行首
Space C-Space 开始选中
C-t 字符调序
杂项:
d 退出 tmux(tmux 仍在后台运行)
t 窗口中央显示一个数字时钟
? 列出所有快捷键
: 命令提示符
配置选项:
# 鼠标支持 – 设置为 on 来启用鼠标
* setw -g mode-mouse off
* set -g mouse-select-pane off
* set -g mouse-resize-pane off
* set -g mouse-select-window off
# 设置默认终端模式为 256color
set -g default-terminal “screen-256color”
# 启用活动警告
setw -g monitor-activity on
set -g visual-activity on
# 居中窗口列表
set -g status-justify centre
# 最大化/恢复窗格
unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp
配置文件(~/.tmux.conf):
# 基础设置
set -g default-terminal “screen-256color”
set -g display-time 3000
set -g escape-time 0
set -g history-limit 65535
set -g base-index 1
set -g pane-base-index 1
# 前缀绑定 (Ctrl+a)
set -g prefix ^a
unbind ^b
bind a send-prefix
# 分割窗口
unbind ‘”‘
bind – splitw -v
unbind %
bind | splitw -h
# 选中窗口
bind-key k select-pane -U
bind-key j select-pane -D
bind-key h select-pane -L
bind-key l select-pane -R
# copy-mode 将快捷键设置为 vi 模式
setw -g mode-keys vi
# 启用鼠标(Tmux v2.1)
set -g mouse on
# 更新配置文件
bind r source-file ~/.tmux.conf \; display “已更新”
——–
和vim一样的思路,需要先安装tmux专属的插件管理器,一般都是用这个:tmux plugin manager,即tpm。注意:文档里面都会提到prefix + …,其中prefix指的是tmux的命令前缀,默认是ctrl+b。
按照官网的做法,很简单就安装上了,输入下面命令:
# 把管理器文件安装到`~/.tmux/plugins/tpm`之下 此前这些目录是不存在的
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# 新建配置文件
vim ~/.tmux.conf
# 将下面内容复制到`~/.tmux.conf`
# List of plugins
set -g @plugin ‘tmux-plugins/tpm’
set -g @plugin ‘tmux-plugins/tmux-sensible’
# Other examples:
# set -g @plugin ‘github_username/plugin_name’
# set -g @plugin ‘git@github.com/user/plugin’
# set -g @plugin ‘git@bitbucket.com/user/plugin’
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run ‘~/.tmux/plugins/tpm/tpm’
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# Tmux Plugin Manager(Tmux v2.1)
# Tmux Resurrect
set -g @plugin ‘tmux-plugins/tmux-resurrect’
# List of plugins
set -g @plugin ‘tmux-plugins/tpm’
set -g @plugin ‘tmux-plugins/tmux-sensible’
# Other examples:
# set -g @plugin ‘github_username/plugin_name’
# set -g @plugin ‘git@github.com/user/plugin’
# set -g @plugin ‘git@bitbucket.com/user/plugin’
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run ‘~/.tmux/plugins/tpm/tpm’
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
系统操作
? 列出所有快捷键;按q返回
d 脱离当前会话;这样可以暂时返回Shell界面,输入tmux attach能够重新进入之前的会话
D 选择要脱离的会话;在同时开启了多个会话时使用
Ctrl+z 挂起当前会话
r 强制重绘未脱离的会话
s 选择并切换会话;在同时开启了多个会话时使用
: 进入命令行模式;此时可以输入支持的命令,例如kill-server可以关闭服务器
[ 进入复制模式;此时的操作与vi/emacs相同,按q/Esc退出
~ 列出提示信息缓存;其中包含了之前tmux返回的各种提示信息
———-
窗口操作
c 创建新窗口
& 关闭当前窗口
数字键 切换至指定窗口
p 切换至上一窗口
n 切换至下一窗口
l 在前后两个窗口间互相切换
w 通过窗口列表切换窗口
, 重命名当前窗口;这样便于识别
. 修改当前窗口编号;相当于窗口重新排序
f 在所有窗口中查找指定文本
———-
面板操作 ” 将当前面板平分为上下两块
% 将当前面板平分为左右两块
x 关闭当前面板
! 将当前面板置于新窗口;即新建一个窗口,其中仅包含当前面板
Ctrl+方向键 以1个单元格为单位移动边缘以调整当前面板大小
Alt+方向键 以5个单元格为单位移动边缘以调整当前面板大小
Space 在预置的面板布局中循环切换;依次包括even-horizontal、even-vertical、main-horizontal、main-vertical、tiled
q 显示面板编号
o 在当前窗口中选择下一面板
方向键 移动光标以选择面板
{ 向前置换当前面板
} 向后置换当前面板
Alt+o 逆时针旋转当前窗口的面板
Ctrl+o 顺时针旋转当前窗口的面板
——
[~]# cat .tmux.conf
set -g default-terminal “screen-256color”
set -g display-time 3000
set -g escape-time 0
set -g history-limit 65535
set -g base-index 1
set -g pane-base-index 1
# 前缀绑定 (Ctrl+a)
set -g prefix ^a
unbind ^b
bind a send-prefix
# 分割窗口
unbind ‘”‘
bind – splitw -v
# – 横向分隔
unbind %
bind | splitw -h
# | 竖向分隔
set -g mouse on