vimrc & tmux 設定

“欸,你都用什麼編輯器?” “小畫家啊”

其實有蠻長一段時間都用sublime text寫code,但不管是在研究所時期或是現在工作以後,幾乎都需要連線到遠端開發機作業,即便可以用掛載磁碟的方式在本地端編輯,但若是加個VPN再參雜一些種花點心的網路…

恩,就這樣我又回到vim的懷抱。以往都直接抓vgod大神的設定檔來改,但你知道改code改到最後都會想說直接重寫還比較快,加上我用到的套件其實不多,所以乾脆自己設定一個。

基本上是使用Vundle這套plug-in manager 來幫我安裝所有的plugin,是我目前使用起來最簡潔的工具了

以下是我的vimrc,可以看得出來基本上是以javascriptreactjs開發為主

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'


Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
Plugin 'tpope/vim-fugitive'
" vim-airline
Plugin 'vim-airline/vim-airline'
Plugin 'elzr/vim-json'
Plugin 'pangloss/vim-javascript'
Plugin 'mattn/emmet-vim'
Plugin 'mxw/vim-jsx'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
"
set nocompatible    " not compatible with the old-fashion vi mode
set bs=2        " allow backspacing over everything in insert mode
set history=50        " keep 50 lines of command line history
set ruler        " show the cursor position all the time
set autoread        " auto read when file is changed from outside
set laststatus=2

syntax on

let g:airline_powerline_fonts = 1
let g:airline_theme='solarized'
syntax enable
set nu

除此之外,既然要遠端連線工作,總不能每次斷線以後,都要手動重新打開上一次工作階段,並且清除temp file吧!

我們是程式設計師,我們很懶,所以需要更聰明的作法。因此tmux就很重要了!

tmux是什麼呢? 根據官方文件:

tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.

看到沒有!

Detach them (they keep running in the background) and reattach them to a different terminal

簡單來說,有了tmux,以後連線到遠端VM或是server時,就算在你開著好幾個session並且vim使用中的情況下,網路斷了或是你想拿著筆電去樓下買咖啡,回來時還是可以無痛回到你原先的工作狀態!不會因為連線中斷而需要重新initialize你原先的state。

那要怎麼使用這麼好用的工具呢?

於 Debian / Ubuntu Linux 安裝 Tmux
1
$ apt-get install tmux

安裝很簡單,接著要編輯tmux.conf,以下是我的設定檔:

unbind C-b
set -g prefix ^A
bind a send-prefix

# set colors
set-window-option -g window-status-current-fg blue
set-window-option -g window-status-current-bg yellow
set-window-option -g window-status-current-attr default

# switch to next window
unbind C-Right
bind -n C-Right next-window

# switch to previous window
unbind C-Left
bind -n C-Left previous-window

# split windows like vim
# vim's definition of a horizontal/vertical split is reversed from tmux's
bind s split-window -v
bind v split-window -h

# move around panes with hjkl, as one would in vim after pressing ctrl-w
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# vi-style controls for copy mode
setw -g mode-keys vi
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

unbind ,
bind A command-prompt "rename-window '%%'"

好,其實也很多是東抓西抓出來的,所以依照我這份設定檔,我常用的指令大概就是:

  • tmux
    進入tmux session

  • tmux attach
    在新的terminal (我推薦Iterm2) 重新attach回你之前的工作狀態(先前開啟中的tmux seesion)

  • Crtl + A & C
    先按Crtl A,接著按下C,就會開啟一個新的tmux session

  • Crtl + A & Shift+7
    其實就是Crtl + A接著按下 & 符號,就可以刪除當前session

用了tmux以後好像世界太平一片祥和風和日麗,但是事情沒有這麼簡單,當你用慣iterms方便的捲軸功能去瀏覽先前的terminal紀錄後,發現開了tmux就捲不上去時可能會很痛苦,不過沒關係,還是有折衷方案!

  • Crtl + A & pageUp or up Arrow
    讓你可以往上或往下去terminal紀錄

好的,老實說這篇只是我想記錄一下我的記錄檔案…
所以如果需要好好學習tmux的人,可以移駕 Tsung’s Blog