tmux 基本配置及使用

Tmux 就是会话与窗口的“解绑”工具:

  1. 它允许在单个窗口中,同时访问多个会话。这对于同时运行多个命令行程序很有用。

  2. 它可以让新窗口”接入”已经存在的会话。

  3. 它允许每个会话有多个连接窗口,因此可以多人实时共享会话。

  4. 它还支持窗口任意的垂直和水平拆分。

类似的终端复用器还有 GNU Screen。Tmux 与它功能相似,但是更易用,也更强大。

本文只介绍基本配置和使用,关于 tmux 的更多内容,可参考:

配置文件

将以下内容拷贝到 ~/.tmux.conf 文件中,没有此文件的请自行创建。

############################################################
# Author: whoru.S.Q <whoru@sqiang.net>
# Link: https://github.com/whorusq/linux-learning/blob/master/tmux/.tmux.conf
# Version: 0.2
############################################################

# ---------- 基本设置 ----------
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
unbind ^b
set -g prefix ^a
bind a send-prefix

# 重新加载配置文件 - prefix r
bind r source ~/.tmux.conf \; display "==> 配置文件重新加载成功!"

# 拆分窗格
unbind '"'
bind - splitw -v # 垂直拆分 - prefix -
unbind %
bind \\ splitw -h # 水平拆分 - prefix \

# 新窗格默认 shell 位置设置为当前目录
bind '"' split-window -c '#{pane_current_path}'
bind '%' split-window -h -c '#{pane_current_path}'

# 窗格切换 - prefix h/j/k/l
bind h select-pane -L # 左边窗格
bind j select-pane -D # 上边窗格
bind k select-pane -U # 下边窗格
bind l select-pane -R # 右边窗格

# 调整窗格大小 - prefix H/J/K/L
bind L resize-pane -L 10 # 向左扩展
bind R resize-pane -R 10 # 向右扩展
bind K resize-pane -U 5 # 向上扩展
bind J resize-pane -D 5 # 向下扩展


# ---------- 显示 ----------

setw -g automatic-rename on # rename window to reflect current program
set -g renumber-windows on # renumber windows when a window is closed

set -g set-titles on # set terminal title
#set -g set-titles-string '#h ❐ #S ● #I #W'
set -g set-titles-string '❐ #S ● #I'

set -g status-interval 10 # redraw status line every 10 seconds


# 状态栏
# 颜色
set -g status-bg black
set -g status-fg white
# 对齐方式
set-option -g status-justify centre
# 左下角
set-option -g status-left '#[bg=black,fg=green][#[fg=cyan]#S#[fg=green]]'
set-option -g status-left-length 20
# 窗口列表
setw -g automatic-rename on
set-window-option -g window-status-format '#[dim]#I:#[default]#W#[fg=grey,dim]'
set-window-option -g window-status-current-format '#[fg=cyan,bold]#I#[fg=blue]:#[fg=cyan]#W#[fg=dim]'
# 右下角
set -g status-right '#[fg=green][#[fg=cyan]%Y-%m-%d#[fg=green]]'

基本使用

终端输入命令

# 查看版本
tmux -V

# 进入
tmux

# 退出
exit
# 或
ctrl + a &

添加了上面的配置文件后,可使用如下快捷键:

按键 功能
ctrl + a - 上下分屏
ctrl + a \ 左右分屏
ctrl + a h 定位到 左 边的分屏
ctrl + a j 定位到 上 边的分屏
ctrl + a k 定位到 下 边的分屏
ctrl + a l 定位到 右 边的分屏
ctrl + a H 增加 左 边分屏的宽度
ctrl + a J 增加 上 边分屏的宽度
ctrl + a K 增加 下 边分屏的宽度
ctrl + a L 增加 右 边分屏的宽度