5個好用的開發(fā)者Vim插件
通過這 5 個插件擴展 Vim 功能來提升你的編碼效率。
我用 Vim 已經超過 20 年了,兩年前我決定把它作為我的首要文本編輯器。我用 Vim 來編寫代碼、配置文件、博客文章及其它任意可以用純文本表達的東西。Vim 有很多超級棒的功能,一旦你適合了它,你的工作會變得非常高效。
在日常編輯工作中,我更傾向于使用 Vim 穩(wěn)定的原生功能,但開源社區(qū)對 Vim 開發(fā)了大量的插件,可以擴展 Vim 的功能、改進你的工作流程和提升工作效率。
以下列舉 5 個非常好用的可以用于編寫任意編程語言的插件。
1、Auto Pairs
Auto Pairs 插件可以幫助你插入和刪除成對的文字,如花括號、圓括號或引號。這在編寫代碼時非常有用,因為很多編程語言都有成對標記的語法,就像圓括號用于函數(shù)調用,或引號用于字符串定義。
Auto Pairs 最基本的功能是在你輸入一個左括號時會自動補全對應的另一半括號。比如,你輸入了一個 [,它會自動幫你補充另一半 ]。相反,如果你用退格鍵刪除開頭的一半括號,Auto Pairs 會刪除另一半。
如果你設置了自動縮進,當你按下回車鍵時 Auto Pairs 會在恰當?shù)目s進位置補全另一半括號,這比你找到放置另一半的位置并選擇一個正確的括號要省勁多了。
例如下面這段代碼:
package mainimport "fmt"func main() {x := trueitems := []string{"tv", "pc", "tablet"}if x {for _, i := range items}}
在 items 后面輸入一個左花括號按下回車會產生下面的結果:
package mainimport "fmt"func main() {x := trueitems := []string{"tv", "pc", "tablet"}if x {for _, i := range items {| (cursor here)}}}
Auto Pairs 提供了大量其它選項(你可以在 GitHub 上找到),但最基本的功能已經很讓人省時間了。
2、NERD Commenter
NERD Commenter 插件給 Vim 增加了代碼注釋的功能,類似在 IDE 中注釋功能。有了這個插件,你可以一鍵注釋單行或多行代碼。
NERD Commenter 可以與標準的 Vim filetype 插件配合,所以它能理解一些編程語言并使用合適的方式來注釋代碼。
最易上手的方法是按 Leader+Space 組合鍵來切換注釋當前行。Vim 默認的 Leader 鍵是 \。
在可視化模式中,你可以選擇多行一并注釋。NERD Commenter 也可以按計數(shù)注釋,所以你可以加個數(shù)量 n 來注釋 n 行。
還有個有用的特性 “Sexy Comment” 可以用 Leader+cs 來觸發(fā),它的塊注釋風格更漂亮一些。例如下面這段代碼:
package mainimport "fmt"func main() {x := trueitems := []string{"tv", "pc", "tablet"}if x {for _, i := range items {fmt.Println(i)}}}
選擇 main 函數(shù)中的所有行然后按下 Leader+cs 會出來以下注釋效果:
package mainimport "fmt"func main() {/** x := true* items := []string{"tv", "pc", "tablet"}** if x {* for _, i := range items {* fmt.Println(i)* }* }*/}
因為這些行都是在一個塊中注釋的,你可以用 Leader+Space 組合鍵一次去掉這里所有的注釋。
NERD Commenter 是任何使用 Vim 寫代碼的開發(fā)者都必裝的插件。
3、VIM Surround
Vim Surround 插件可以幫你“環(huán)繞”現(xiàn)有文本插入成對的符號(如括號或雙引號)或標簽(如 HTML 或 XML 標簽)。它和 Auto Pairs 有點兒類似,但是用于處理已有文本,在編輯文本時更有用。
比如你有以下一個句子:
"Vim plugins are awesome !"
當你的光標處于引起來的句中任何位置時,你可以用 ds" 組合鍵刪除句子兩端的雙引號。
Vim plugins are awesome !
你也可以用 cs"' 把雙端的雙引號換成單引號:
'Vim plugins are awesome !'
或者再用 cs'[ 替換成中括號:
[ Vim plugins are awesome ! ]
它對編輯 HTML 或 XML 文本中的標簽尤其在行。假如你有以下一行 HTML 代碼:
<p>Vim plugins are awesome !</p>
當光標在 “awesome” 這個單詞的任何位置時,你可以按 ysiw<em> 直接給它加上著重標簽(<em>):
<p>Vim plugins are <em>awesome</em> !</p>
注意它聰明地加上了 </em> 閉合標簽。
Vim Surround 也可以用 ySS 縮進文本并加上標簽。比如你有以下文本:
<p>Vim plugins are <em>awesome</em> !</p>
你可以用 ySS<div class="normal"> 加上 div 標簽,注意生成的段落是自動縮進的。
<div class="normal"><p>Vim plugins are <em>awesome</em> !</p></div>
Vim Surround 有很多其它選項,你可以參照 GitHub 上的說明嘗試它們。
4、Vim Gitgutter
Vim Gitgutter 插件對使用 Git 作為版本控制工具的人來說非常有用。它會在 Vim 的行號列旁顯示 git diff 的差異標記。假設你有如下已提交過的代碼:
1 package main23 import "fmt"45 func main() {6 x := true7 items := []string{"tv", "pc", "tablet"}89 if x {10 for _, i := range items {11 fmt.Println(i)12 }13 }14 }
當你做出一些修改后,Vim Gitgutter 會顯示如下標記:
1 package main23 import "fmt"4_ 5 func main() {6 items := []string{"tv", "pc", "tablet"}7~ 8 if len(items) > 0 {9 for _, i := range items {10 fmt.Println(i)+ 11 fmt.Println("------")12 }13 }14 }
_ 標記表示在第 5 行和第 6 行之間刪除了一行。~ 表示第 8 行有修改,+ 表示新增了第 11 行。
另外,Vim Gitgutter 允許你用 [c 和 ]c 在多個有修改的塊之間跳轉,甚至可以用 Leader+hs 來暫存某個變更集。
這個插件提供了對變更的即時視覺反饋,如果你用 Git 的話,有了它簡直是如虎添翼。
5、VIM Fugitive
Vim Fugitive 是另一個將 Git 工作流集成到 Vim 中的超棒插件。它對 Git 做了一些封裝,可以讓你在 Vim 里直接執(zhí)行 Git 命令并將結果集成在 Vim 界面里。這個插件有超多的特性,更多信息請訪問它的 GitHub 項目頁面。
這里有一個使用 Vim Fugitive 的基礎 Git 工作流示例。設想我們已經對下面的 Go 代碼做出修改,你可以用 :Gblame 調用 git blame 來查看每行***的提交信息:
e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 1 package maine9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 2e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 3 import "fmt"e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 4e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│_ 5 func main() {e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 6 items := []string{"tv", "pc", "tablet"}e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 700000000 (Not Committed Yet 2018-12-05 18:55:00 -0500)│~ 8 if len(items) > 0 {e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 9 for _, i := range items {e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 10 fmt.Println(i)00000000 (Not Committed Yet 2018-12-05 18:55:00 -0500)│+ 11 fmt.Println("------")e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 12 }e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 13 }e9949066 (Ricardo Gerardi 2018-12-05 18:17:19 -0500)│ 14 }
可以看到第 8 行和第 11 行顯示還未提交。用 :Gstatus 命令檢查倉庫當前的狀態(tài):
1 # On branch master2 # Your branch is up to date with 'origin/master'.3 #4 # Changes not staged for commit:5 # (use "git add <file>..." to update what will be committed)6 # (use "git checkout -- <file>..." to discard changes in working directory)7 #8 # modified: vim-5plugins/examples/test1.go9 #10 no changes added to commit (use "git add" and/or "git commit -a")--------------------------------------------------------------------------------------------------------1 package main23 import "fmt"4_ 5 func main() {6 items := []string{"tv", "pc", "tablet"}7~ 8 if len(items) > 0 {9 for _, i := range items {10 fmt.Println(i)+ 11 fmt.Println("------")12 }13 }14 }
Vim Fugitive 在分割的窗口里顯示 git status 的輸出結果。你可以在該行按下 - 鍵用該文件的名字暫存這個文件的提交,再按一次 - 可以取消暫存。這個信息會隨著你的操作自動更新:
1 # On branch master2 # Your branch is up to date with 'origin/master'.3 #4 # Changes to be committed:5 # (use "git reset HEAD <file>..." to unstage)6 #7 # modified: vim-5plugins/examples/test1.go8 #--------------------------------------------------------------------------------------------------------1 package main23 import "fmt"4_ 5 func main() {6 items := []string{"tv", "pc", "tablet"}7~ 8 if len(items) > 0 {9 for _, i := range items {10 fmt.Println(i)+ 11 fmt.Println("------")12 }13 }14 }
現(xiàn)在你可以用 :Gcommit 來提交修改了。Vim Fugitive 會打開另一個分割窗口讓你輸入提交信息:
1 vim-5plugins: Updated test1.go example file2 # Please enter the commit message for your changes. Lines starting3 # with '#' will be ignored, and an empty message aborts the commit.4 #5 # On branch master6 # Your branch is up to date with 'origin/master'.7 #8 # Changes to be committed:9 # modified: vim-5plugins/examples/test1.go10 #
按 :wq 保存文件完成提交:
[master c3bf80f] vim-5plugins: Updated test1.go example file1 file changed, 2 insertions(+), 2 deletions(-)Press ENTER or type command to continue
然后你可以再用 :Gstatus 檢查結果并用 :Gpush 把新的提交推送到遠程。
1 # On branch master2 # Your branch is ahead of 'origin/master' by 1 commit.3 # (use "git push" to publish your local commits)4 #5 nothing to commit, working tree clean
Vim Fugitive 的 GitHub 項目主頁有很多屏幕錄像展示了它的更多功能和工作流,如果你喜歡它并想多學一些,快去看看吧。
接下來?
這些 Vim 插件都是程序開發(fā)者的神器!還有另外兩類開發(fā)者常用的插件:自動完成插件和語法檢查插件。它些大都是和具體的編程語言相關的,以后我會在一些文章中介紹它們。
你在寫代碼時是否用到一些其它 Vim 插件?請在評論區(qū)留言分享。






















