強化tmux操作

承襲前面的基本的tmux操作,知道在Ubuntu或Linux下,有一個強大的terminal工具。

但原生tmux有很多不足,這邊多介紹一些tmux的強化功能,讓tmux更容易使用

強化tmux操作

以下內容,都是在編輯.tmux.conf
(一般是放在家目錄下)
順序上,把外掛的安裝以及設定放在最後
其他tmux本身的設定放前面

更改環境設定

  1. 使用ctrl + a取代ctrl + b
set-option -g prefix C-a  
  1. 調整成彩色的console畫面
set -g default-terminal "screen-256color"
  1. 調整pane-window和window起始數值
set -g base-index 1
set -g pane-base-index 1
  1. 限制history大小
set -g history-limit 10000
  1. copy-mode改成類似vi的操作模式
set-window-option -g mode-keys vi
  1. 設定操作時間
set -sg escape-time 0

更改按鍵設定

預設的kill-pane-window是 ctrl + x
所以讓kill-window成為ctrl + X
方便記憶

bind X confirm-before -p "kill this window? (y/n)"\
             kill-window

prefix + c有時因為小指頭不靈活,不是很好按,所以增加一個prefix + <ctrl + c>其實比較順

# More c or C-c to create window
bind C-c new-window

切換window設定成 n, p 較方便記憶
bind -r能持續按鍵效果,否則要連續切換時要重壓兩次

# No need to press prefix + n,p everytime
bind -r C-n next-window
bind -r C-p previous-window

切換上個window很常用,設成連續敲兩次prefix

# Prevent typo and make similar way as screen
bind C-a last-window

仿照vim,用hjkl切換分割視窗

## Make pane-moved with k,j,h,l
bind -r C-k select-pane -U
bind -r C-j select-pane -D
bind -r C-h select-pane -L
bind -r C-l select-pane -R

安裝外掛

// 開啟以及管理tmux外掛的工具

安裝tpm

set -g @plugin 'tmux-plugins/tpm'

安裝外掛的範例

# 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'
按鍵 說明
prefix + I 安裝外掛
prefix + U 更新安裝外掛
prefix + <alt + u> 移除外掛

// 讓tmux的操作環境更合理一點 (sensible)

安裝tmux-sensible

set -g @plugin 'tmux-plugins/tmux-sensible'

有許多設定會自動完成,沒有快捷鍵
若有自行設定的快捷鍵
tmux-sensible會保留而不會覆蓋

// 讓複製貼上更強大

copycat能夠在tmux中的console畫面,搜尋特定樣式的字串,而不用滑鼠反白進行複製貼上
安裝tmux-copycat

set -g @plugin 'tmux-plugins/tmux-copycat'
按鍵 說明
prefix + / 搜尋特定字串 (支持正規化)
prefix + ctrl-f 搜尋’檔案’的樣式
prefix + ctrl-u 搜尋url的樣式
prefix + ctrl-d 搜尋’數字’的樣式
prefix + ctrl-g git status中搜尋檔案
N or n 向上 or 向下搜尋
Enter 複製反白文字 (vi-mode)
prefix + ] 貼上

// 提示prefix按鍵作用的警示

安裝tmux-prefix-highlight

set -g @plugin 'tmux-plugins/tmux-prefix-highlight'

設定色彩

set -g @prefix_highlight_fg 'white' # default is 'colour231'
set -g @prefix_highlight_bg 'blue'  # default is 'colour04'

設定會highlight的模式及樣式

set -g @prefix_highlight_show_copy_mode 'on'
set -g @prefix_highlight_copy_mode_attr 'fg=black,bg=yellow,bold' # default is 'fg=default,bg=yellow'
set -g @prefix_highlight_show_sync_mode 'on'
set -g @prefix_highlight_sync_mode_attr 'fg=black,bg=green' # default is 'fg=default,bg=yellow'

其他請參考網站

// 不用離開tmux就能快速操作session

安裝tmux-sessionist

set -g @plugin 'tmux-plugins/tmux-sessionist'
按鍵 說明
prefix + C 開啟新的session
prefix + X 刪除現在的session而非detach
prefix + S 跳到最後一個session
prefix + @ 將現有pane加入到新session

// 更方便操作pane視窗

安裝tmux-pain-control

set -g @plugin 'tmux-plugins/tmux-pain-control'
按鍵 說明
prefix + h, j, k, l 在pane window間 [左 上 下 右] 移動
prefix + ctrl + [h, j, k, l] 同上
prefix + shift + [h, j, k, l] 調整視窗邊界 [左 上 下 右] 移動
prefix + [|, -] 垂直或水平切割視窗

// 讓複製貼上不限於tmux內

在ubuntu上運行有個安裝前的要求,得先安裝xsel or xclip

sudo apt-get install xsel # or xclip

之後才加入安裝tmux-yank

# Make the copied text in tmux could be paste outside the tmux
set -g @plugin 'tmux-plugins/tmux-yank'
按鍵 說明
prefix + y 複製最後一行command結果到系統clipboard
prefix + Y 複製目前資料夾的路徑到系統clipboard
(copy-mode下) y 複製tmux畫面文字到系統clipboard
(copy-mode下) Y 把tmux畫面的文字,輸入到command
不會用到複製,單純讀到畫面什麼文字就輸入該文字

// 儲存 / 回存 session

這很好用,在下班前save
上班前restore,就可以延續先前的工作
安裝tmux-resurrect

# Used for save/restore session after reboot
set -g @plugin 'tmux-plugins/tmux-resurrect'
按鍵 說明
prefix + Ctrl-s save
prefix + Ctrl-r restore

其他

更多設定可參考Reference

對「強化tmux操作」的一則回應

  1. 引用通告: 常用 tmux – 小倉庫

發表留言