新聞中心
通過這 5 個插件擴展 Vim 功能來提升你的編碼效率。

永春網站建設公司創(chuàng)新互聯,永春網站設計制作,有大型網站制作公司豐富經驗。已為永春成百上千提供企業(yè)網站建設服務。企業(yè)網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的永春做網站的公司定做!
我用 Vim 已經超過 20 年了,兩年前我決定把它作為我的首要文本編輯器。我用 Vim 來編寫代碼、配置文件、博客文章及其它任意可以用純文本表達的東西。Vim 有很多超級棒的功能,一旦你適合了它,你的工作會變得非常高效。
在日常編輯工作中,我更傾向于使用 Vim 穩(wěn)定的原生功能,但開源社區(qū)對 Vim 開發(fā)了大量的插件,可以擴展 Vim 的功能、改進你的工作流程和提升工作效率。
以下列舉 5 個非常好用的可以用于編寫任意編程語言的插件。
1、Auto Pairs
Auto Pairs 插件可以幫助你插入和刪除成對的文字,如花括號、圓括號或引號。這在編寫代碼時非常有用,因為很多編程語言都有成對標記的語法,就像圓括號用于函數調用,或引號用于字符串定義。
Auto Pairs 最基本的功能是在你輸入一個左括號時會自動補全對應的另一半括號。比如,你輸入了一個 [,它會自動幫你補充另一半 ]。相反,如果你用退格鍵刪除開頭的一半括號,Auto Pairs 會刪除另一半。
如果你設置了自動縮進,當你按下回車鍵時 Auto Pairs 會在恰當的縮進位置補全另一半括號,這比你找到放置另一半的位置并選擇一個正確的括號要省勁多了。
例如下面這段代碼:
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 增加了代碼注釋的功能,類似在 IDEintegrated development environment 中注釋功能。有了這個插件,你可以一鍵注釋單行或多行代碼。
NERD Commenter 可以與標準的 Vim filetype 插件配合,所以它能理解一些編程語言并使用合適的方式來注釋代碼。
最易上手的方法是按 Leader+Space 組合鍵來切換注釋當前行。Vim 默認的 Leader 鍵是 \。
在可視化模式Visual mode中,你可以選擇多行一并注釋。NERD Commenter 也可以按計數注釋,所以你可以加個數量 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 函數中的所有行然后按下 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)繞”現有文本插入成對的符號(如括號或雙引號)或標簽(如 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 文本中的標簽tag尤其在行。假如你有以下一行 HTML 代碼:
Vim plugins are awesome !
當光標在 “awesome” 這個單詞的任何位置時,你可以按 ysiw 直接給它加上著重標簽():
Vim plugins are awesome !
注意它聰明地加上了 閉合標簽。
Vim Surround 也可以用 ySS 縮進文本并加上標簽。比如你有以下文本:
Vim plugins are awesome !
你可以用 Vim plugins are awesome ! Vim Surround 有很多其它選項,你可以參照 GitHub 上的說明嘗試它們。 Vim Gitgutter 插件對使用 Git 作為版本控制工具的人來說非常有用。它會在 Vim 的行號列旁顯示 當你做出一些修改后,Vim Gitgutter 會顯示如下標記: 另外,Vim Gitgutter 允許你用 這個插件提供了對變更的即時視覺反饋,如果你用 Git 的話,有了它簡直是如虎添翼。 Vim Fugitive 是另一個將 Git 工作流集成到 Vim 中的超棒插件。它對 Git 做了一些封裝,可以讓你在 Vim 里直接執(zhí)行 Git 命令并將結果集成在 Vim 界面里。這個插件有超多的特性,更多信息請訪問它的 GitHub 項目頁面。 這里有一個使用 Vim Fugitive 的基礎 Git 工作流示例。設想我們已經對下面的 Go 代碼做出修改,你可以用 可以看到第 8 行和第 11 行顯示還未提交。用 Vim Fugitive 在分割的窗口里顯示 現在你可以用 按 然后你可以再用 Vim Fugitive 的 GitHub 項目主頁有很多屏幕錄像展示了它的更多功能和工作流,如果你喜歡它并想多學一些,快去看看吧。 這些 Vim 插件都是程序開發(fā)者的神器!還有另外兩類開發(fā)者常用的插件:自動完成插件和語法檢查插件。它些大都是和具體的編程語言相關的,以后我會在一些文章中介紹它們。 你在寫代碼時是否用到一些其它 Vim 插件?請在評論區(qū)留言分享。ySSdiv 標簽,注意生成的段落是自動縮進的。
4、Vim Gitgutter
git diff 的差異標記。假設你有如下已提交過的代碼:
1 package main 2 3 import "fmt" 4 5 func main() { 6 x := true 7 items := []string{"tv", "pc", "tablet"} 8 9 if x { 10 for _, i := range items { 11 fmt.Println(i) 12 } 13 } 14 }
1 package main 2 3 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 行。[c 和 ]c 在多個有修改的塊之間跳轉,甚至可以用 Leader+hs 來暫存某個變更集。5、VIM Fugitive
: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 }:Gstatus 命令檢查倉庫當前的狀態(tài):
1 # On branch master 2 # Your branch is up to date with 'origin/master'. 3 # 4 # Changes not staged for commit: 5 # (use "git add 6 # (use "git checkout -- 7 # 8 # modified: vim-5plugins/examples/test1.go 9 # 10 no changes added to commit (use "git add" and/or "git commit -a")-------------------------------------------------------------------------------------------------------- 1 package main 2 3 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 }git status 的輸出結果。你可以在該行按下 - 鍵用該文件的名字暫存這個文件的提交,再按一次 - 可以取消暫存。這個信息會隨著你的操作自動更新:
1 # On branch master 2 # Your branch is up to date with 'origin/master'. 3 # 4 # Changes to be committed: 5 # (use "git reset HEAD 6 # 7 # modified: vim-5plugins/examples/test1.go 8 #-------------------------------------------------------------------------------------------------------- 1 package main 2 3 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 }:Gcommit 來提交修改了。Vim Fugitive 會打開另一個分割窗口讓你輸入提交信息:
1 vim-5plugins: Updated test1.go example file 2 # Please enter the commit message for your changes. Lines starting 3 # with '#' will be ignored, and an empty message aborts the commit. 4 # 5 # On branch master 6 # Your branch is up to date with 'origin/master'. 7 # 8 # Changes to be committed: 9 # modified: vim-5plugins/examples/test1.go 10 #:wq 保存文件完成提交:
[master c3bf80f] vim-5plugins: Updated test1.go example file 1 file changed, 2 insertions(+), 2 deletions(-)Press ENTER or type command to continue:Gstatus 檢查結果并用 :Gpush 把新的提交推送到遠程。
1 # On branch master 2 # 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接下來?
文章題目:5個好用的開發(fā)者Vim插件
標題路徑:http://fisionsoft.com.cn/article/ccedcgg.html


咨詢
建站咨詢
