Skip to content

Instantly share code, notes, and snippets.

@Godsing
Created May 19, 2021 08:27
Show Gist options
  • Save Godsing/c817baaeb44ceaaf57c91f047eaf5501 to your computer and use it in GitHub Desktop.
Save Godsing/c817baaeb44ceaaf57c91f047eaf5501 to your computer and use it in GitHub Desktop.
Tmux 配置文件(保姆级注释,含部分插件)
# 1. 安装tpm: mkdir -p ~/.tmux/plugins && cd ~/.tmux/plugins && git clone https://github.com/tmux-plugins/tpm
# 2. 按 prefix + I(大写) 来安装插件
# 3. 安装 vim-obsession, 用 vundle 安装或: cd ~/.vim/bundle && git clone git://github.com/tpope/vim-obsession.git --depth 1 && vim -u NONE -c "helptags vim-obsession/doc" -c q
##### 以上需手工执行 #####
## 修改 tmux-prefix 键: ctrl+b --> ctrl+a
set -g prefix C-a
unbind C-b
bind a send-prefix
#set-option -g prefix2 `
## 基础配置
set -g default-terminal "screen-256color" # 开启 256 colors 支持
set -g display-time 3000
set -g history-limit 65535
## 鼠标支持
set -g mouse on
## 面板相关
# 分隔面板
unbind '"'
bind - splitw -v -c '#{pane_current_path}'
unbind %
bind | splitw -h -c '#{pane_current_path}'
# 面板(pane)切换
bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R
bind -r e lastp # 选择最后一个面板
bind -r ^e last # 选择最后一个窗口
bind -r ^u swapp -U # 与前一个面板交换位置
bind -r ^d swapp -D # 与后一个面板交换位置
# -r表示可重复按键,大概500ms之内,重复的h、j、k、l按键都将有效,完美支持了快速切换的Geek需求
# 调整面板大小:绑定 Ctrl + h/j/k/l 键调整pane边缘
bind -r ^k resizep -U 10 # upward (prefix Ctrl+k)
bind -r ^j resizep -D 10 # downward (prefix Ctrl+j)
bind -r ^h resizep -L 10 # to the left (prefix Ctrl+h)
bind -r ^l resizep -R 10 # to the right (prefix Ctrl+l)
## 复制模式
setw -g mode-keys vi # 设置为vi编辑模式
bind Escape copy-mode # 绑定esc键为进入复制模式
bind -t vi-copy v begin-selection # 绑定v键为开始选择文本
bind -t vi-copy y copy-selection # 绑定y键为复制选中文本
## 其他杂项配置
bind r source-file ~/.tmux.conf \; display-message "source-file ~/.tmux.conf done" # 绑定重载 settings 的热键
bind m command-prompt "splitw -h 'exec man %%'" # 绑定m键为在新的panel打开man
## 一些锦上添花的优化
# 设置窗口面板起始序号
set -g base-index 1 # 设置窗口(window)的起始下标为1
set -g pane-base-index 1 # 设置面板(pane)的起始下标为1
# 关闭默认的rename机制
setw -g automatic-rename off # tmux默认会自动重命名窗口,频繁的命令行操作,将频繁触发重命名,比较浪费CPU性能
setw -g allow-rename off
# 关闭默认窗口标题
#set -g set-titles off
# 自定义状态栏
set -g status-utf8 on # 状态栏支持utf8
set -g status-interval 1 # 状态栏刷新时间
set -g status-justify left # 状态栏列表左对齐
setw -g monitor-activity on # 非当前窗口有内容更新时在状态栏通知
#set -g status-bg black # 设置状态栏背景黑色
#set -g status-fg yellow # 设置状态栏前景黄色
#set -g status-style "bg=black, fg=yellow" # 状态栏前景背景色
set -g status-left "#[bg=#88cc44] Sess: #S " # 状态栏-左侧内容 (#S: 会话(session)名称)
#set -g status-right 'Continuum status: #{continuum_status}' # 状态栏右侧内容
set -g status-left-length 300 # 状态栏-左边长度300
set -g status-right-length 500 # 状态栏-左边长度500
set -wg window-status-format " #I:#W " # 状态栏-(当前窗口除外的其他)窗口名称格式
set -wg window-status-separator "" # 状态栏-窗口名称之间的间隔
set -wg window-status-current-format " #I:#W #F " # 状态栏-当前窗口名称格式(#I:序号,#w:窗口名称,#F:间隔符)
set -wg window-status-current-style "bg=black, fg=yellow" # 状态栏-当前窗口名称的背景色
#set -wg window-status-last-style "fg=black" # 状态栏最后一个窗口名称的样式
set -g message-style "bg=#202529, fg=#91A8BA" # 指定消息通知的前景、后景色
########## 插件 ##########
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
# tmux-resurrect 插件,用于手动保存和恢复 tmux 环境信息。
# 使用方式:按 PREFIX,Ctrl+s 保存 session,按 PREFIX,Ctrl+r 恢复 session
set -g @plugin 'tmux-plugins/tmux-resurrect'
# 使 tmux-resurrect 支持保存和恢复 tmux 中的 vim 会话:
set -g @resurrect-strategy-vim 'session' # 需要 VIM 先安装 https://github.com/tpope/vim-obsession 插件
# tmux-continuum 插件。依赖: tmux>=1.9, bash, tmux-resurrect
# 每隔15分钟自动保存 tmux 环境信息(不需要任何配置,默认就有)
set -g @plugin 'tmux-plugins/tmux-continuum'
# 使得机器启动时,自动启动 tmux server:
set -g @continuum-boot 'on' # This is only for Linux. For MacOSX, see: https://github.com/tmux-plugins/tmux-continuum/blob/master/docs/automatic_start.md
# tmux server 启动时,自动加载上次保存的环境信息
set -g @continuum-restore 'on'
# 初始化 tmux 插件管理器(保证这行在 ~/.tmux.conf 的非常靠后的位置)
run '~/.tmux/plugins/tpm/tpm'
# 安装插件:
# 1. 在 ~/.tmux.conf 中增加新的插件,如: set -g @plugin 'GitHub用户名/仓库名'。
# 2. 按 PREFIX + I(大写) 键,将自动下载插件,并刷新tmux环境。
# 更新插件: 按 PREFIX + U(大写) 键,选择要更新的插件后,回车确认。
# 卸载插件:
# 1. 在 ~/.tmux.conf 中移除插件所在行。
# 2. 按下 PREFIX + Alt + u(小写) 移除插件。
## 参考资料:
# Tmux使用手册 - http://louiszhai.github.io/2017/09/30/tmux/
@Godsing
Copy link
Author

Godsing commented Jul 8, 2021

⚠️ 注意 !!!
Tmux 2.8 及之后的版本,如下几行会报错,请自行注释掉:
bind -t vi-copy v begin-selection # 绑定v键为开始选择文本
bind -t vi-copy y copy-selection # 绑定y键为复制选中文本
set -g status-utf8 on # 状态栏支持utf8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment