Vim Plugin - Tabular 安裝

gaorongchao1990626發表於2013-09-16


  1. git clone https://github.com/godlygeek/tabular.git
  2. cd tabular/
  3. mv after autoload doc plugin ~/.vim/
  4. vim ~/.vimrc # 於此檔案最末端加入下述
    1. let mapleader=','
    2. if exists(":Tabularize")
    3.     nmap <Leader>a= :Tabularize /=<CR>
    4.     vmap <Leader>a= :Tabularize /=<CR>
    5.     nmap <Leader>a: :Tabularize /:\zs<CR>
    6.     vmap <Leader>a: :Tabularize /:\zs<CR>
    7. endif
    8. inoremap <silent> <Bar>   <Bar><Esc>:call <SID>align()<CR>a
    9. function! s:align()
    10.     let p = '^\s*|\s.*\s|\s*$'
    11.     if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
    12.         let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
    13.         let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
    14.         Tabularize/|/l1
    15.         normal! 0
    16.         call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
    17.     endif
    18. endfunction

Tabular Plugin 測試

輸入下述內容:

|start|eat|left|
|12|5|7|
|20|5|15|

V 選取後, , 之後在下面繼續打, 都會自動排序. (若文字過長, 上面都會自動對齊)

:Tab/ 後面接要分隔的符號, ex: 上述是用 "|" 分隔, 所以輸入 :Tab/|, 程式若要排 "=", 則輸入 :Tab/=, 更多範例可見下述:

  • :Tab/:
  • :Tab/=
  • :Tab/:\zs # 只有排序 ":" 後面的文字

相關文章