作者:crane-yuan 日期:2017-03-03
前言
tmuxinator是tmux的配置管理工具,解決了tmux服務器關機后session丟失問題。tmuxinator可以根據配置文件快速創建tmux的session。
Tmuxinator的安裝
Tmuxinator基于Ruby,首先安裝Ruby
Ubuntu用戶可以用apt-get命令安裝:
> apt-get install ruby
ArchLinux用戶可以用pacman命令安裝:
> pacman -S ruby
安裝Tmuxinator
若由于(你懂得的)網絡原因無法安裝,則更新Ruby的gem源后再次嘗試。
> gem source -a https://ruby.taobao.org/
> gem source -r https://rubygems.org/
安裝tmuxinator
> gem install tmuxinator
基礎設置
bash版
將下述文本保存為$HOME/.tmuxinator/.tmuxinator.bash,提供bash的tab鍵提示功能
#!/usr/bin/env bash
_tmuxinator() {
COMPREPLY=()
local word
word="${COMP_WORDS[COMP_CWORD]}"
if [ "$COMP_CWORD" -eq 1 ]; then
local commands="$(compgen -W "$(tmuxinator commands)" -- "$word")"
local projects="$(compgen -W "$(tmuxinator completions start)" -- "$word")"
COMPREPLY=( $commands $projects )
elif [ "$COMP_CWORD" -eq 2 ]; then
local words
words=("${COMP_WORDS[@]}")
unset words[0]
unset words[$COMP_CWORD]
local completions
completions=$(tmuxinator completions "${words[@]}")
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
fi
}
complete -F _tmuxinator tmuxinator mux
$HOME/.bashrc下增加下述內容:
source $HOME/.tmuxinator/.tmuxinator.bash
export EDITOR='vim'
source $HOME/.bashrc使其生效。
zsh版
將下述文本保存為$HOME/.tmuxinator/.tmuxinator.zsh,提供zsh的tab鍵提示功能
_tmuxinator() {
local commands projects
commands=(${(f)"$(tmuxinator commands zsh)"})
projects=(${(f)"$(tmuxinator completions start)"})
if (( CURRENT == 2 )); then
_describe -t commands "tmuxinator subcommands" commands
_describe -t projects "tmuxinator projects" projects
elif (( CURRENT == 3)); then
case $words[2] in
copy|debug|delete|open|start)
_arguments '*:projects:($projects)'
;;
esac
fi
return
}
$HOME/.zshrc下增加下述內容:
source $HOME/.tmuxinator/.tmuxinator.zsh
export EDITOR='vim'
source $HOME/.zshrc使其生效。
常用命令
Tmuxinator的一個工程(Project)對應tmux的一個session。
tmuxinator命令已alias為mux。
new簡寫為n,open簡寫為o,edit簡寫為e,list簡寫為l,copy簡寫為c,delete簡寫為d。
> mux n ws # 創建工程ws
> mux o ws # 打開工程ws的配置文件
> mux e ws # 同上
> mux c ws ws1 # 復制ws工程到ws1
> mux d ws # 刪除ws工程
> mux l # 顯示所有工程
> mux ws # 開啟ws工程
配置
當new一個工程后,會出現如下信息(省略注釋)。
name: ws # session名稱
root: ~/ # 工程根目錄,活動Pane會首先cd到此目錄
windows:
- editor: # 第1個名為Editor的Window
layout: main-vertical # Pane的布局
panes: # 各個Pane
- vim # 第一個Pane運行vim命令
- guard # 第二個Pane運行guard命令
- server: bundle exec rails s # 第2個名為server的Window,運行命令為bundle
- logs: tail -f log/development.log # 第3個名為logs的Window,運行命令為tail
可以根據注釋配置自己的工程。
自定義layout
工程配置中的layout項,有5個默認的值。
- even-horizontal
- even-vertical
- main-horizontal
- main-vertical
- tiled
開啟tmux后,可以使用快捷鍵prefix space切換layout,建議開啟4個Pane進行測試。
下面簡單演示下這5個默認布局的樣式,面板配置如下:
panes:
- top
- top
- vim .
- vim .
even-horizontal
even-vertical
main-horizontal
main-vertical
tiled
其中main-horizontal和main-vertical可以設置默認主Pane的寬度和高度。
在$HOME/.tmux.conf文件中添加下面這些內容:
set-window-option -g main-pane-width 100 # 設置主Pane寬度
set-window-option -g main-pane-height 80 # 設置主Pane高度
如果不滿足layout默認值,layout項可以自定義值。
首先調整好窗口的Pane,prefix d關閉Session。
> tmux list-windows
1: bash* (4 panes) [211x47] [layout 9a0a,211x47,0,0{110x47,0,0,12,100x47,111,0[100x23,111,0,13,100x23,111,24{49x23,111,24,14,50x23,161,24,15}]}] @3 (active)
將上述layout之后的信息(到最后一個]前),復制到工程配置中的layout項即可。注意pane的個數必須與執行命令的個數對應。
windows:
- editor:
layout: 9a0a,211x47,0,0{110x47,0,0,12,100x47,111,0[100x23,111,0,13,100x23,111,24{49x23,111,24,14,50x23,161,24,15}]}
- # empty
- # empty
- # empty
- # empty
多命令
當某個Pane需要執行多命令時,官方不推薦使用&&或;的形式。可以采用如下方式發送命令。
windows:
- editor:
layout: main-vertical
panes:
- list: # 多命令方式
- cd ~/temp
- ls -la
- # empty