安装方法
- CentOs: yum -y install tmux
- MaxOs: brew install tmux
窗格操作
- % 左右平分出两个窗格
- " 上下平分出两个窗格
- x 关闭当前窗格
- { 当前窗格前移
- } 当前窗格后移
- ; 选择上次使用的窗格
- o 选择下一个窗格,也可以使用上下左右方向键来选择
- space 切换窗格布局,tmux 内置了五种窗格布局,也可以通过 ⌥1 至 ⌥5来切换
- z 最大化当前窗格,再次执行可恢复原来大小
- q 显示所有窗格的序号,在序号出现期间按下对应的数字,即可跳转至对应的窗格
-
:setw synchronize-panes
同步窗格
窗口操作
tmux 除了窗格以外,还有窗口(window) 的概念。依次使用以下快捷键来熟悉 tmux 的窗口操作:
-
c
新建窗口,此时当前窗口会切换至新窗口,不影响原有窗口的状态 -
p
切换至上一窗口 -
n
切换至下一窗口 -
w
窗口列表选择,注意 macOS 下使用⌃p
和⌃n
进行上下选择 -
&
关闭当前窗口 -
,
重命名窗口,可以使用中文,重命名后能在 tmux 状态栏更快速的识别窗口 id -
0
切换至 0 号窗口,使用其他数字 id 切换至对应窗口 -
f
根据窗口名搜索选择窗口,可模糊匹配
会话操作
如果运行了多次 tmux
命令则会开启多个 tmux 会话(session)。在 tmux 会话中,使用前缀快捷键 ⌃b
配合以下快捷键可操作会话:
-
$
重命名当前会话 -
s
选择会话列表 -
d
detach 当前会话,运行后将会退出 tmux 进程,返回至 shell 主进程
在 shell 主进程下运行以下命令可以操作 tmux 会话:
tmux new -s foo # 新建名称为 foo 的会话
tmux ls # 列出所有 tmux 会话
tmux a # 恢复至上一次的会话
tmux a -t foo # 恢复名称为 foo 的会话,会话默认名称为数字
tmux kill-session -t foo # 删除名称为 foo 的会话
tmux kill-server # 删除所有的会话
除以上提到的快捷键以外,tmux 还有许多其他的快捷键和命令,使用前缀快捷键 `⌃b` 加 `?` 可以查看所有的快捷键列表,该列表视图为 **tmux copy 模式**,该模式下可使用以下快捷键(无需加 `⌃b` 前缀):
-
⌃v
下一页 -
Meta v
上一页 (tmux 快捷键为 Emacs 风格,这里的 Meta 键可用 Esc 模拟) -
⌃s
向前搜索 -
q
退出 copy 模式 -
:new
启动新会话 -
s
列出所有绘画 -
$
重命名当前会话
配置选项
# -----------------------------------------------------------------------------
# Tmux 基本配置 - 要求 Tmux >= 2.3
# 如果不想使用插件,只需要将此节的内容写入 ~/.tmux.conf 即可
# -----------------------------------------------------------------------------
# C-b 和 VIM 冲突,修改 Prefix 组合键为 Control-Z,按键距离近
set -g prefix C-z
set -g status-justify centre # 居中窗口列表
set -g base-index 1 # 窗口编号从 1 开始计数
set -g display-panes-time 10000 # PREFIX-Q 显示编号的驻留时长,单位 ms
set -g mouse on # 开启鼠标
set -g pane-base-index 1 # 窗格编号从 1 开始计数
set -g renumber-windows on # 关掉某个窗口后,编号重排
setw -g allow-rename off # 禁止活动进程修改窗口名
setw -g automatic-rename off # 禁止自动命名新窗口
setw -g mode-keys vi # 进入复制模式的时候使用 vi 键位(默认是 EMACS)
# -----------------------------------------------------------------------------
# 使用插件 - via tpm
# 1. 执行 git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# 2. 执行 bash ~/.tmux/plugins/tpm/bin/install_plugins
# -----------------------------------------------------------------------------
setenv -g TMUX_PLUGIN_MANAGER_PATH '~/.tmux/plugins'
# 推荐的插件(请去每个插件的仓库下读一读使用教程)
set -g @plugin 'seebi/tmux-colors-solarized'
set -g @plugin 'tmux-plugins/tmux-pain-control'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tpm'
# tmux-resurrect
set -g @resurrect-dir '~/.tmux/resurrect'
# tmux-prefix-highlight
set -g status-right '#{prefix_highlight} #H | %a %Y-%m-%d %H:%M'
set -g @prefix_highlight_show_copy_mode 'on'
set -g @prefix_highlight_copy_mode_attr 'fg=white,bg=blue'
# 初始化 TPM 插件管理器 (放在配置文件的最后)
run '~/.tmux/plugins/tpm/tpm'