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

简介: Vim 是 Linux 下一款很常用的文本编辑器,虽然它对初学者而言并不友好,但通过一些插件的配合,它可以被打造成一款很强大的 IDE 。良许曾经介绍过三款很常用的插件,可点击以下链接查看:Vim 编辑器的 3 款实用插件本文再介绍一款 Vim 编辑器中的一款很强大插件—— VIM Fugitive 。这款插件可以实现你在 Vim 编辑器里直接完成 Git 操作,而无需退出 Vim 。更多 Linux 精选干货电子书,可私聊我 「资料」获取。这个插件是开源项目,我们可以在以下地址获取源码:安装方法:cd ~/.vim/bundlegit clone vim -u NON

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

Vim 编辑器的 3 款实用插件

本文再介绍一款 Vim 编辑器中的一款很强大插件—— VIM Fugitive 。这款插件可以实现你在 Vim 编辑器里直接完成 Git 操作,而无需退出 Vim 。更多 Linux 精选干货电子书,可私聊我 「资料」获取。

这个插件是开源项目,我们可以在以下地址获取源码:

安装方法:

cd ~/.vim/bundle
git clone
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 ,直接就可以看到改动:

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 ..." to update what will be committed)
6 # (use "git checkout -- ..." 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 类似。

2020 精选 阿里/腾讯等一线大厂 面试、简历、进阶、电子书 私聊我「资料」免费获取

在下半屏里,有三个符号:_ 表示在第 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天前
|
开发工具 git
记IDEA Git版本回退并push到远程操作
记IDEA Git版本回退并push到远程操作
31 1
记IDEA Git版本回退并push到远程操作
|
1天前
|
开发工具 git 开发者
|
1天前
|
开发工具 git
web后端-IDEA的Git操作
web后端-IDEA的Git操作
|
1天前
|
Linux 网络安全 开发工具
Git拉取代码的完整示例操作
Git拉取代码的完整示例操作
82 0
|
1天前
|
Shell 开发工具 git
git相关操作
git相关操作
|
1天前
|
Unix Linux 开发工具
【Linux】VIM命令模式和文本输入模式切换操作
【1月更文挑战第18天】【Linux】VIM命令模式和文本输入模式切换操作
|
1天前
|
开发工具 git 开发者
【专栏】探讨了 Git 中的 `git rebase` 操作,它用于重新应用提交到另一分支,改变历史顺序
【4月更文挑战第29天】本文探讨了 Git 中的 `git rebase` 操作,它用于重新应用提交到另一分支,改变历史顺序。与 `git merge` 不同,rebase 重写提交历史,提供简洁线性的历史记录。文章介绍了 rebase 的基本操作、应用场景,如整理提交历史、解决冲突和整合分支,并强调了使用注意事项,如避免在公共分支上操作。尽管 rebase 可以带来整洁的历史和冲突解决便利,但其潜在的风险和可能导致的历史混乱需谨慎对待。理解并恰当使用 `git rebase` 可以提升开发效率和代码质量。
|
1天前
|
程序员 开发工具
【专栏】Vim是知名文本编辑器,以其高效操作深受程序员喜爱
【4月更文挑战第28天】Vim是知名文本编辑器,以其高效操作深受程序员喜爱。本文到特定行的技巧,包括基础概念(普通模式与插入模式)、使用`G`命令、命令行、相对行号和搜索功能。此外,还分享了高级技巧,如使用标记和结合插件提升效率。掌握这些方法能提升Vim用户的工作效率。
|
1天前
|
Linux 开发工具 git
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
|
1天前
|
开发工具 git