Vim 是 Linux 下一款很常用的文本編輯器,雖然它對初學者而言並不友好,但通過一些插件的配合,它可以被打造成一款很強大的 IDE 。良許曾經介紹過三款很常用的插件,可點擊以下鏈接查看:
本文再介紹一款 Vim 編輯器中的一款很強大插件—— VIM Fugitive 。這款插件可以實現你在 Vim 編輯器裡直接完成 Git 操作,而無需退出 Vim 。更多 Linux 精選乾貨電子書,可私聊我 「資料」獲取。
這個插件是開源項目,我們可以在以下地址獲取源碼:
<code>https://github.com/tpope/vim-fugitive/<code>
安裝方法:
<code>cd ~/.vim/bundle
git clone https://github.com/tpope/vim-fugitive.git
vim -u NONE -c "helptags vim-fugitive/doc" -c q/<code>
現在進行一些基本功能演示。假如現在有這麼一段代碼:
<code> 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 }/<code>
現在我們將第 6 行刪除,再修改第 9 行,同時在 11 行後添加一行代碼。現在我們想查看這些修改,按往常做法,我們是先保存文檔再退出,然後執行 git status 。
但現在,我們不必退出,直接在命令模式下輸入 :Gstatus ,直接就可以看到改動:
<code> 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 <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.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 }/<file>/<file>/<code>
如結果所示,Vim Fugitive 打開了一個有上下分屏的界面,上面一半,跟我們平常執行 git status 看到的結果一樣,下面一半,就是具體發動內容,跟 git diff 類似。
2020 精選 阿里/騰訊等一線大廠 面試、簡歷、進階、電子書 私聊我「資料」免費獲取
在下半屏裡,有三個符號:_ 表示在第 5 行與第 6 行之間有代碼被刪除,~ 表示在第 8 行代碼被修改過,+ 表示 11 行新增了代碼。
同樣的,我們可以查看每行代碼是誰改的,可以用 git blame ,而在這裡對應的是 Gblame 。
<code>e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 1 package main
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 2
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 3 import "fmt"
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 4
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│_ 5 func main() {
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 6 items := []string{"tv", "pc", "tablet"}
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 7
00000000 (Not Committed Yet 2019-6-7 18:55:00 -0500)│~ 8 if len(items) > 0 {
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 9 for _, i := range items {
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 10 fmt.Println(i)
00000000 (Not Committed Yet 2019-6-7 18:55:00 -0500)│+ 11 fmt.Println("------")
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 12 }
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 13 }
e9949066 (Alvin Yan 2019-6-7 18:17:19 -0500)│ 14 }/<code>
我們同樣也看到第 8 和 11 行還沒有提交。
現在我們想要提交我們的改動,可以敲入 :Gcommit 命令。Vim Fugitive 將打開另外一塊區域,我們可以在裡面寫入要提交的信息。
<code> 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 #/<code>
然後我們就可以執行 :wq 結束提交。
<code>[master c3bf80f] vim-5plugins: Updated test1.go example file
1 file changed, 2 insertions(+), 2 deletions(-)
Press ENTER or type command to continue/<code>
我們同樣可以繼續使用 :Gstatus 來查看提交後的狀態,也可以使用 :Gpush 將提交推送到遠程倉庫。
<code> 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/<code>
以上這些是 Vim Fugitive 最基礎的用法,如果想學習它的更高級用法,可以去它的 Github倉庫查看,那裡有更詳細的教程。
本文首發於良許Linux教程網 lxlinux.net
閱讀更多 良許Linux 的文章