Tmux 學習摘要2--配置

配置文件

系統範圍的配置文件是 /etc/tmux.conf,而針對我的的配置文件在 ~/.tmux.confphp

經過配置文件,能夠定義新的快捷鍵,定義窗口、窗格佈局等。shell

綁定 CAPS LOCK 鍵到 CTRL 鍵

在 OS X 上:打開 Keyboard preference panel->System Preference,按下 Modifier 鍵,而後將 CAPS LOCK 的動做改成 Controlvim

在 Linux,需對鍵盤配置文件進行修改:windows

sudo vi /etc/default/keyboard

找到以 XKBOPTIONS 開頭的行,添加 ctrl:nocaps 使 CAPS LOCK 成爲另外一個 CTRL 鍵,或者添加 ctrl:swapcaps 使 CAPS LOCK 鍵和 CTRL 兩鍵的功能相互交換。 例如,修改後的內容可能爲:bash

XKBOPTIONS="lv3:ralt_alt,compose:menu,ctrl:nocaps"

而後運行:app

sudo dpkg-reconfigure keyboard-configuration

詳細請參考 Emacs WIKI編輯器

定義一個更加容易按的 PREFIX

CTRL-b 不太好按,若是已將 CAPS LOCK 重定義爲 CTRL,CTRL-a 會好按的多(GNU-SCreen 的PREFIX 也是 CTRL-a)。ide

.tmux.conf 中的配置命令是 set-option ,或者簡寫爲 set佈局

將 tmux PREFIX 從新定義:測試

set -g prefix C-a

這裏的 -g 開關是 global 全局的意思,表示該設置值做用於全部的 tmux 會話。

可使用 unbind-key 命令,或者簡寫的 unbind 命令來取消以前的綁定,如取消以前的 PREFIX 綁定 CTRL-b

unbind C-b

由於從新綁定後,以前的綁定會自動取消,所以在本例中,沒有必要用 unbind

當配置文件 .tmux.conf 修改後,tmux 並不會自動進行從新讀取和執行,須要在 tmux 會話中,使用快捷鍵 PREFIX : 進入命令模式,並執行 source-file ~/.tmux.conf 來從新加載配置文件 。

修改發送命令的默認延時時長

向 tmux 發送命令的默認延時時長很是短,可能會致使與 Vim 等編輯器衝突。

能夠將延時時長調高些,以提升操做響應能力:

set -sg escape-time 1

窗口的默認編號是從 0 開始的,因爲鍵盤上 0 和 1 的排列位置相關較遠,,最好將窗口設置成從 1 開始編號:

set -g base-index 1

set 命令是針對會話的配置命令,而針對窗口的配置命令是 set-window-option,或者簡寫爲 setw。因爲窗格是窗口中的事物,要將窗格的默認編號也設置成從1 開始,應該用 setw 命令:

setw -g pane-base-index 1

自定義按鍵、命令和用戶輸入

tmux 的大部分快捷鍵都過長,或者難以操做。應該將經常使用的快捷鍵從新進行設置。

 建立一個從新加載配置文件的快捷鍵

每次修改配置文件後,之前都須要執行 source-file ~/.tmux.conf,能夠將這一命令定義成一個快捷鍵 PREFIX r

bind r source-file ~/.tmux.conf

當從新加載後,最好要有消息提醒,能夠用 display 命令在狀態欄輸出消息提醒。bind 命令能夠綁定多條命令,各命令間用 \; 分開,如:

bind r source-file ~/.tmux.conf \; display "Reloaded!"

這樣的綁定在使用時須要加前綴, 若是不要前綴,則:

bind-key -n C-r source-file ~/.tmux.conf

向會話內的應用發送 PREFIX

CTRL-a 也是 Vim, Bash 的快捷鍵,所以須要設置一個快捷鍵來向其它應用發送 CTRL-a

bind C-a send-prefix

綁定後,只需按兩次CTRL-a 就能向其它應用程序發送 CTRL-a 了。

窗格分割命令

默認的分割命令很難記,綁定能形象記憶的快捷鍵:

bind | split-window -h #水平分隔
bind - split-window -v

窗格間的移動

窗格間默承認以用 PREFIX o 進行循環切換,或者用 PREFIX 方向鍵 進行切換。參照 Vim, 用 hjkl 進行切換:

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

同時設置用 PREFIX CTRL-hPREFIX CTRL-l 在窗口間進行切換移動:

bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

這裏的 -r 開關是 repeatable 可重複的意思,表示只需按一次 PREFIX, 後面可屢次連續按綁定鍵。默認的間隔時間是 500 毫秒,能夠設置 repeat-time 來修改。

設計修改窗格大小

bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

禁用鼠標

在 tmux 2.1 如下版本:

setw -g mode-mouse off

在 tmux 2.1 及以上版本:

setw -g mouse off

外觀風格

顏色配置

確保 Tmux 和終端都支持 256 色。

在終端中測試是否支持 256 色:

$wget http://www.vim.org/scripts/download_script.php?src_id=4568 -O colortest.pl
$perl colortest.pl -w

在 Linux 上,可能須要在 .bashrc 中添加:

[ -z "$TMUX" ] && export TERM=xterm-256color

Mac Snow Leopard 的終端應用只支持 16 色, 需安裝 iTerm2 支持更多顏色, 在 iTerm2 中,打開 default profile,將 Terminal mode 修改爲 xterm-256color, 同時確保終端支持 UTF-8。

爲使 tmux 能顯示 256 色:

set -g default-terminal "screen-256color"

修改顏色

修改狀態欄顏色

set -g status-fg white
set -g status-bg black

修改窗口列表顏色

setw -g window-status-fg cyan
setw -g window-status-bg default
setw -g window-status-attr dim

setw -g window-status-current-fg white
setw -g window-status-current-bg red
setw -g window-status-current-attr bright

修改窗格分隔欄顏色

set -g pane-border-fg green
set -g pane-border-bg black
set -g pane-active-border-fg white
set -g pane-active-border-bg yellow

定製命令行

set -g message-fg white
set -g message-bg black
set -g message-attr bright

定製狀態欄

狀態欄支持的變量

變量 描述

H | 本地主機名

h | 不含域名的主地主機名

F | 當前窗口符號

l | 當前窗口的索引號

P | 當前窗格的索引號

S | 當前會話名

T | 當前窗口的標題

W | 當前窗口的名稱

| # 字符

(shell-command) | Shell 命令的首行輸出

[attributes] | 顏色或屬性值設置

總結

命令總結

命令 描述
set -g prefix C-a 設置 PREFIX 鍵
set -sg escape-time n 設置按 PREFIX 鍵後,等待按鍵的毫秒數。
source-file [file] 從新加載配置文件
bind C-a send-prefix 按兩次 PREFIX 將向其它應用發送 PREFIX 鍵組合
bind-key key 建立執行指定命令的快捷鍵,可簡寫爲 bind
bind-key -r key 建立執行指定命令的快捷鍵,可簡寫爲 bind,該快捷鍵可在一次按下 PREFIX 後,進行屢次連續鍵入
unbind-key [key] 取消快捷鍵綁定,可簡寫爲 unbind
display-message 或 display 在狀態欄顯示信息
set-option flags[value] 爲會話設置,使用 -g 開關爲全部會話設置
set-window-option option 設置窗口屬性
set-a 將設置值添加現有選項上,而不進行替換

來自 Pragmatic Tmux 的配置文件:

# workflows/tmux.conf
# Our .tmux.conf file

# Setting the prefix from C-b to C-a
set -g prefix C-a

# Free the original Ctrl-b prefix keybinding
unbind C-b

#setting the delay between prefix and command
set -sg escape-time 1

# Ensure that we can send Ctrl-A to other apps
bind C-a send-prefix

# Set the base index for windows to 1 instead of 0
set -g base-index 1

# Set the base index for panes to 1 instead of 0
setw -g pane-base-index 1

# Reload the file with Prefix r
bind r source-file ~/.tmux.conf \; display "Reloaded!"

# splitting panes
bind | split-window -h
bind - split-window -v

# moving between panes
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Quick pane selection
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# Pane resizing
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# mouse support - set to on if you want to use the mouse
setw -g mode-mouse off
set -g mouse-select-pane off
set -g mouse-resize-pane off
set -g mouse-select-window off

# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"

# enable activity alerts
setw -g monitor-activity on
set -g visual-activity on

# set the status line's colors
set -g status-fg white
set -g status-bg black

# set the color of the window list
setw -g window-status-fg cyan
setw -g window-status-bg default
setw -g window-status-attr dim

# set colors for the active window
setw -g window-status-current-fg white
setw -g window-status-current-bg red
setw -g window-status-current-attr bright

# pane colors
set -g pane-border-fg green
set -g pane-border-bg black
set -g pane-active-border-fg white
set -g pane-active-border-bg yellow

# Command / message line
set -g message-fg white
set -g message-bg black
set -g message-attr bright

# Status line left side
set -g status-left-length 40
set -g status-left "#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P"

set -g status-utf8 on

# Status line right side
# 15% | 28 Nov 18:15
set -g status-right "#(~/battery Discharging) | #[fg=cyan]%d %b %R"

# Update the status bar every sixty seconds
set -g status-interval 60

# Center the window list
set -g status-justify centre

# enable vi keys.
setw -g mode-keys vi

# Open panes in the same directory using the tmux-panes script
unbind v
unbind n
bind v send-keys " ~/tmux-panes -h" C-m
bind n send-keys " ~/tmux-panes -v" C-m

# Maximize and restore a pane
unbind Up
bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp

# Log output to a text file on demand
bind P pipe-pane -o "cat >>~/#W.log" \; display "Toggled logging to ~/#W.log"

參考資源:

tmux: Productive Mouse-Free Development

相關文章
相關標籤/搜索