沒穿方服

索引

顯示╱隱藏內文

Vim 簡短入門提示

艱難學習至今,提出三項。


<Esc> 難按

切換 normal mode 是最硬的基本技,<Esc> 不快你立馬被打爆。
可以用 <C-[> 取代,或配合 <Leader>(預設是 \,Enter 鍵是一字或 L 型會影響其手感)或 <LocalLeader> 鍵。
例:按兩下 , 即進入 normal mode

let maplocalleader = ","
noremap  <LocalLeader>, <C-\><C-N>
noremap! <LocalLeader>, <C-\><C-N>

早裝早輕鬆的插件

GSession 配合 git branch(不過 Windows 下比較難使用 git)是管理 session 的快樂方案。參考 Vim-Taiwan 上的討論串
因為多檔編輯需求大,但 session 操作繁瑣,所以認定首要插件就此一支。


學會查 help

大量招式(睇 :help quickref)不是單純常用就能學會的,已知指令也值得翻文複習。畢竟軟體演變下來,誰比較懂得善用靠的還是知識。

  • 更新 help

    如果 Vim 版本很舊,可考慮更新整個 $VIMRUNTIME(包含 help 以外的各種檔案,所以會花點時間)。
    以下為系統有 aap 支援的情形:
    :!cd $VIMRUNTIME
    :!aap -f ftp://ftp.vim.org/pub/vim/runtime/main.aap fetch
    另外安裝插件等操作動到 doc 檔案時,也需要重建一下 tag(讀 help 時用來 jump 的、類似超連結的東西)
    :helptags ~/.vim/doc
    (其中 ~/.vim/doc 可能是其他位置,即放說明文件的地方)
  • 益於求助的 map

    預設 <F1> 等同於 :help 其實不易利用,可改為:

    • 於 normal mode 查詢游標所在單字的說明。
    • 於 visual mode 查詢選取文字的說明,對付 'sb' 這般有引號的項目較方便。
    • 以上若先按 <LocalLeader> 則將說明開在新分頁。
    map  <F1> :help <C-R>=expand('<cword>')<CR><CR>
    map  <LocalLeader><F1> :tab help <C-R>=expand('<cword>')<CR><CR>
    xmap <F1> :<C-U>call RegStash(1)<CR>gvy:let b:tempReg=@"<CR>:call RegStash()<CR>:help <C-R>=b:tempReg<CR><CR>
    xmap <LocalLeader><F1> :<C-U>call RegStash(1)<CR>gvy:let b:tempReg=@"<CR>:call RegStash()<CR>:help <C-R>=b:tempReg<CR><CR>
    (其中 RegStash 來自暫存與還原 register 的 function,也可移掉)

    在 help 文件中,預設可用 <C-]> 跳到游標所在單字的說明(類似超連結),按 <C-O><C-I> 則是在次次跳轉的位置來回(類似上一頁、下一頁),這裡也可稍微 map:

    • 若檔案唯讀(help 文件皆唯讀),就讓 Enter 也可跳進 tag、BS 也可回上一頁。
    • Alt-LeftAlt-Right 當成上一頁、下一頁使用。
    nnoremap <expr> <CR> &modifiable ? "i<CR><C-\><C-N>" : "<C-]>"
    nnoremap <expr> <BS> &modifiable ? "i<C-W><C-\><C-N>" : "<C-O>"
    nnoremap <M-Left> <C-O>
    nnoremap <M-Right> <C-I>

註:以上只是讓動作變簡單,要真正愛用 help,還是要親身體會、自求多福~

部分操作會將相關資訊暫存備用,例如 d 掉的文字會被放進 register 變數,以便之後貼上。

執行 vim script(例如自訂的 map)時若做了這類操作,便可能把本來的 register 等資料蓋掉。以下幾個 function 先將暫存資訊另存,以供 script 結束後還原。

原始碼

gist: 555806 - [.vimrc] simple functions to save/restore position, register or mark.

使用例

:call PosStash(1)   " 暫存目前游標位置
:call PosStash()    " 還原游標位置
:call RegStash(1)   " 暫存目前 register
:call RegStash()    " 還原 register
:call MarkStash(1)  " 暫存目前 mark
:call MarkStash()   " 還原 mark

一般選擇連續多行的註解時,會按 V 進入 visual mode,然後移動游標框住想要的;行數一多,懶惰就出現了。

選取整個註解區是件懊懆事

以下簡陋模擬一個註解用的 text object,方便在 normal mode 按 vac 選取整塊註解(不含空行);同樣按 dac 刪除、yac 複製。

gist: 554634 - [.vimrc] Simple text object for continuous comment

實作上只判斷每行的第一個非空白字元是否被 syntax highlight 為 Comment,所以並不十分可靠,也僅能做 linewise 的選取。若要改善功能,可考慮導入 'comments''commentstring''filetype' 等選項(參考 EnhancedCommentifytComment 的做法),不過複雜度的增加也令人卻步。

附 tComment 用的 map,若游標在註解區就反轉整塊註解,否則照常 :TComment。

map <silent><expr> <LocalLeader>ca IsInComment() ? "vac:TComment<CR>" : ":TComment<CR>"

以前都靠 PSPad 用來製作 bookmarklet 的簡易 script,現在改讓 Vim 處理。

:Bookmarklet 沒錯誤的話即出現此視窗

這個 funcion 需要上一篇的 Vim funcion:使用 closure compiler 壓縮 js 文件,還有 Vim 的 ruby 支援(其實只是用來 encodeURI ……)

原始碼展開如下,但以後更新會放在 gist: 546828 - [.vimrc] make bookmarklet

command! -nargs=* Bookmarklet call Bookmarklet(<f-args>)
fun! Bookmarklet(...)
  let result = JsCompress(0, 0, '--compilation_level=WHITESPACE_ONLY')
  if len(getqflist()) == 0 && strlen(result) > 0
    let reuse_win = 0
    for winnr in tabpagebuflist(tabpagenr())
      if bufname(winnr) == '[Bookmarklet]'
        exec winnr . 'wincmd w'
        let reuse_win = winnr
      endif
    endfor
    if ! reuse_win
      exec 'belowright 5new [Bookmarklet]'
      setlocal noswapfile bufhidden=wipe winfixheight filetype=javascript
    endif
    let result = substitute(result, '[\n]', '', '')
    setlocal buftype=
    call setline(1, 'javascript:(function(){' . result . '})();')
    if has('ruby')
      ruby require("uri"); VIM::Buffer.current.line = URI.escape(VIM::Buffer.current.line);
    else
      echohl WarningMsg | echomsg "No ruby support. Can't do URI encoding." | echohl None
    endif
    setlocal buftype=nofile
  endif
endf

註:僅測試於 Cygwin console Vim + Windows 版的 java,其他環境只測過 Win32 gVim ——失敗。

通用的解決方案請參考 othree 寫的 Vim 儲存完 JavaScript 檔案後自動用 yuicompressor
本篇特徵為:

  • 只使用 google closure-compiler
  • 採用 :make 執行,故能以 Quick Fix 視窗除錯。

    warning_level 設 DEFAULT 的話,壓 MooTools 居然不過……

  • 壓縮前未存檔、實行壓縮前、……幾個場合能先詢問確認。

    不存檔是壓什麼意思的? 若不詢問,每次 :w 就自動跑也是很費時的

  • 壓縮結果可選擇存到改名的檔案(傳回檔名)或存到暫存檔(傳回壓縮後的文字)。
    自動命名規則為 foo.js → foo.min.js 或 foo-debug.js → foo.js。

使用方式

  1. 把本文原始碼加進 .vimrc。 gist: 545665 - [.vimrc] function to use closure-compiler
  2. 下載 closure-compiler 將 jar 放到程式找得到的地方(如程式第一行 let jar = '/scripts/google-compiler-20100616.jar'
  3. 幾種用法:
    • function:call JsCompress(save [, interact [, options ]])
      save: 若為 1 則自動命名存檔,0 則存到暫存檔。
      interact: 若為 1,執行 make 前會詢問確認。
      options: compiler 用的參數字串,例如 '--compilation_level=WHITESPACE_ONLY'。這裡留意若不指定的話, warning_level 用的並非預設值而是 QUITE,因為 warnning 就會有 quickfix 而中斷壓縮。
      :call JsCompress(1, 0, '--warning_level=DEFAULT')
    • command:JsCompress[!] [interact] [options]
      同上但 save 參數被 ! (bang) 取代。
      :JsCompress! 1 '--compilation_level=WHITESPACE_ONLY'
    • autocmd:在 .vimrc 加上 au FileWritePost,BufWritePost *-debug.js :JsCompress! 1
      以後編完 foo-debug.js 存檔,便會詢問是否壓縮。

已知問題

中文檔名在 make 出錯時,quickfix 和產生的新檔是亂碼。

懷疑是 Windows java 的問題所以先不處理