如何在 Vim 里直接完成 Git 操做?

Vim 是 Linux 下一款很经常使用的文本编辑器,虽然它对初学者而言并不友好,但经过一些插件的配合,它能够被打形成一款很强大的 IDE 。良许曾经介绍过三款很经常使用的插件,可点击如下连接查看:linux

Vim 编辑器的 3 款实用插件git

本文再介绍一款 Vim 编辑器中的一款很强大插件—— VIM Fugitive 。这款插件能够实现你在 Vim 编辑器里直接完成 Git 操做,而无需退出 Vim 。更多 Linux 精选干货电子书,可在公众号「良许Linux」后台回复 「资料」获取。github

这个插件是开源项目,咱们能够在如下地址获取源码:面试

https://github.com/tpope/vim-fugitive复制代码

安装方法:vim

cd ~/.vim/bundle
git clone https://github.com/tpope/vim-fugitive.git
vim -u NONE -c "helptags vim-fugitive/doc" -c q复制代码

如今进行一些基本功能演示。假如如今有这么一段代码:编辑器

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 }复制代码

如今咱们将第 6 行删除,再修改第 9 行,同时在 11 行后添加一行代码。如今咱们想查看这些修改,按往常作法,咱们是先保存文档再退出,而后执行 git status 。学习

但如今,咱们没必要退出,直接在命令模式下输入 :Gstatus ,直接就能够看到改动:spa

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 }复制代码

如结果所示,Vim Fugitive 打开了一个有上下分屏的界面,上面一半,跟咱们日常执行 git status 看到的结果同样,下面一半,就是具体发动内容,跟 git diff 相似。.net

2020 精选 阿里/腾讯等一线大厂 面试、简历、进阶、电子书 公众号「良许Linux」后台回复「资料」免费获取插件

在下半屏里,有三个符号:_ 表示在第 5 行与第 6 行之间有代码被删除,~ 表示在第 8 行代码被修改过,+ 表示 11 行新增了代码。

一样的,咱们能够查看每行代码是谁改的,能够用 git blame ,而在这里对应的是 Gblame

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 }复制代码

咱们一样也看到第 8 和 11 行尚未提交。

如今咱们想要提交咱们的改动,能够敲入 :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复制代码

以上这些是 Vim Fugitive 最基础的用法,若是想学习它的更高级用法,能够去它的 Github仓库查看,那里有更详细的教程。

看完的都是真爱,点个赞再走呗?您的「三连」就是良许持续创做的最大动力!

  1. 关注原创公众号「良许Linux」,第一时间获取最新Linux干货!
  2. 公众号后台回复【资料】【面试】【简历】获取精选一线大厂面试、自我提高、简历等资料。
  3. 关注个人博客:lxlinux.net