自動給tmux現場作備胎

tmux系列文章:linux



還記得本渣之前寫的給tmux現場作備胎的腳本嗎?其實後來本渣就沒再去拓展這個腳本了,不是由於以前的腳本運行得夠好不須要再改了,而是在寫好那個腳本那年,有一個工具橫空出世,讓本渣以爲不再用造輪子了——好了,不賣關子了,這個工具就是tmuxresurrect插件git

resurrect官方有一個使用錄屏,怎麼樣?狂拽酷炫屌!咱們想要有的功能基本都有了吧!並且resurrect還部分支持恢復panel裏運行的程序啊有木有!要知道這並很差實現,之前本渣寫備胎腳本時也折騰過,當時還只能在恢復panel時給出上次運行程序的提示。
不過resurrect插件須要手動保存和恢復tmux現場,若是須要自動化的話,能夠再配合continuum插件來使用。github

好了,軟文就寫到這麼多了,下面咱們來看看如何安裝使用。shell

首先查看tmux版本:vim

tmux -V

resurrect插件須要tmux1.9以上的版本。若是你的版本低於1.9,那麼升級是必須的,其實把tmux升級到1.9以上仍是蠻推薦的。工具

這裏推薦安裝tpm (Tmux Plugin Manager)tmux插件管理,再經過tpm安裝continuum等插件:this

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

編輯~/.tmux.conf,在文件末尾加入如下幾行:spa

# Set default shell to zsh
# set-option -g default-shell /bin/zsh

# Use the following line to fix OS X tmux problems
# set-option -g default-command "reattach-to-user-namespace -l zsh"

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# 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'

# Enable automatic restore
set -g @continuum-restore 'on'

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

若是你的默認shell是zsh,請把這句的註釋去掉:插件

set-option -g default-shell /bin/zsh

若是你用的是Mac OSX,把這句的註釋也去掉:vagrant

set-option -g default-command "reattach-to-user-namespace -l zsh"

這主要是tmux在OSX下水土不服(更詳細的問題描述能夠看這篇文章:Reattach-to-user-namespace: The Fix for Your Tmux in OS X Woes),須要用reattach-to-user-namespace黑科技,因此你最好也用MacPorts或者Homebrew裝下這個工具:

port install tmux-pasteboard
brew install reattach-to-user-namespace

在終端下執行如下命令更新tmux配置,運行tpm

tmux source ~/.tmux.conf

最後在tmux下運行prefix + I(若是你沒改鍵綁定的話就是<Ctrl>-b + <Shift>-i)安裝插件。你還能夠經過prefix + U<Ctrl>-b + <Shift>-u)更新插件,用prefix + <Alt> + u刪除插件。

安裝完成以後,你能夠在~/.tmux/plugins/裏找到每一個插件的代碼目錄。resurrect插件還有一個run_tests腳本用於檢查是否安裝正確,不過要運行這個腳本須要裝上虛擬化神器vagrant

若是安裝正確,continuum插件會每隔15分鐘產生一份備胎,咱們也能夠用prefix + <Ctrl>-s手動備份,用prefix + <Ctrl>-r手動恢復——vimer吐槽一記,這熱鍵定義得好emacs風...

下面咱們來看看resurrect插件產生的tmux環境備胎。在tmux環境備胎生成以後,在~/.tmux/resurrect/目錄下就會有名爲tmux_resurrect_$(date).txt的文本文件,同時會有一個叫last的軟連接指向最新的tmux_resurrect_$(date).txt。那麼這個神祕的txt文件裏有啥呢?這裏本渣隨意貼上一份:

pane    0       0       :apt-fast       1       :*      0       :/home/ys    1       sudo  :sudo apt-fast install linux-image-3.13.0-70-generic-dbgsym
pane    0       1       :~      0       :       0       :/home/ys    1       zsh     :
pane    0       2       :~      0       :-      0       :/home/ys    1       zsh     :
window  0       0       1       :*      c51d,95x28,0,0,0
window  0       1       0       :       c51e,95x28,0,0,1
window  0       2       0       :-      c51f,95x28,0,0,2
state   0

啊哈,看起來思路和本渣以前腳本思路差很少嘛!只不過resurrect插件記錄的信息更詳細,還作了鍵綁定。resurrect的代碼也不難懂,若是你對它的實現感興趣的話能夠去閱讀一下啦~

相關文章
相關標籤/搜索