1. 程式人生 > 其它 >Tmux 終端複用器美化(2021/11/20)

Tmux 終端複用器美化(2021/11/20)

Tmux 終端複用器美化(2021/11/20)

目錄

1. 簡介

參考《Tmux 基本使用》

2. 配置

2.1 修改字首鍵

Tmux 擁有大量的快捷鍵,所有的快捷鍵都需要通過字首鍵喚起,預設字首鍵為 Ctrl + b,兩鍵相隔較遠使用起來不方便,筆者喜歡修改為 Ctrl

+ x,修改方法如下:

# 編輯 tmux 的使用者配置檔案,路徑為 ~/.tmux.conf,如果沒有手動建立
$ vim ~/.tmux.conf
# 加入以下內容
set-option -g prefix2 C-x

注:該操作只是添加了第二個字首鍵,並不會影響原字首鍵。

2.2 開啟滑鼠支援

Tmux 支援分屏操作,在各窗格之間切換焦點及更改窗格大小時預設通過字首鍵加方向鍵來實現,在窗格較多時並不方便,故筆者喜歡開啟滑鼠支援(不會影響鍵盤操作),開啟方法如下:

# 編輯 tmux 的使用者配置檔案,路徑為 ~/.tmux.conf,如果沒有手動建立
$ vim ~/.tmux.conf
# 加入以下內容
set-option -g mouse on

2.3 修改複製模式快捷鍵風格

在 Tmux 中操作文字自然離不開復制模式,通過使用複製模式的過程如下:

  1. 按下 prefix + [ 進入複製模式;
  2. 按下 Space 開始選擇文字,移動游標選擇複製區域;
  3. 按下 Enter 複製選中文字並退出複製模式;
  4. 按下 prefix + ] 貼上文字;

Tmux 中複製模式預設的快捷鍵風格為emacs,筆者更加熟悉vim,故改為vim快捷鍵風格,修改方法如下:

# 檢視 tmux 複製模式快捷鍵風格
$ tmux show-window-options -g mode-keys

# 編輯 tmux 配置檔案,設定為 vi 風格
$ vim .tmux.conf
# 加入以下內容,可以設定為 vi 或 emacs
set-window-option -g mode-keys vi

更多配置參考Tmux使用手冊 | louis blog (louiszhai.github.io)

3. 外掛

3.1 tpm(Tmux Plugin Manager)

3.1.1 介紹

tpm 是一個 Tmux 外掛管理工具,用於安裝和管理 Tmux 外掛,使外掛安裝變得更加簡單。

3.1.2 安裝

# 通過 git 拉取 tpm,如果遇到網路可以通過 https://hub.fastgit.org 映象下載
# git clone https://hub.fastgit.org/tmux-plugins/tpm ~/.tmux/plugins/tpm
$ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

注:tpm 使用要求 Tmux 版本等於 1.9。

3.1.3 配置

tpm 配置檔案與 Tmux 相同,路徑為~/tmux.conf(或$XDG_CONFIG_HOME/tmux/tmux.conf),編輯該檔案新增 tpm 相關的配置,具體操作如下:

# 修改 tmux 配置
$ vim ~/.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 'github_username/plugin_name#branch'
# set -g @plugin '[email protected]:user/plugin'
# set -g @plugin '[email protected]:user/plugin'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

# 過載 tmux 配置
$ tmux source ~/.tmux.conf

3.1.4 外掛安裝

  1. 按照 set -g @plugin '...' 的格式將新外掛新增到 .tmux.conf 檔案;
  2. 按下 prefix + I 下載安裝外掛;

3.1.5 外掛解除安裝

  1. 將外掛從 .tmux.conf 檔案移除;
  2. 按下 prefix + Alt + u 解除安裝外掛;

3.1.6 快捷鍵

  • prefix + I:安裝外掛並重新整理 Tmux 環境。
  • prefix + u:更新外掛。
  • prefix + Alt + u:解除安裝外掛。

3.2 外掛推薦

此處不再詳細介紹每個外掛的功能與作用,所有外掛均可在 Github 搜尋下載。

set -g @plugin 'arcticicestudio/nord-tmux'
# set -g @plugin 'tmux-plugins/tmux-battery'
# set -g @plugin 'tmux-plugins/tmux-cpu'
# set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
# set -g @plugin 'tmux-plugins/tmux-online-status'
# set -g @plugin 'tmux-plugins/tmux-net-speed'
# set -g @plugin 'tmux-plugins/tmux-sidebar'