Using Git From Vim
Git. Everyone loves it. Everyone hates it. Hardly anyone can resist it.
You have just read my best article intro of all time.
Using Git from within Vim can be very beneficial: you don't need to switch context or jump between windows. With some help from our friends (plugins), we can do almost anything from Vim.
You probably want to edit your commit messages in Vim.
To do it, add the
EDITOR environment variable.
Put this into your
As an alternative, set the editor in
~/.gitconfig (this option has a priority over
git config --global core.editor nvim
git config command writes the settings into the
~/.gitconfig file. You can edit that file directly as well.
Terminal and external commands
Modern Vim has a built-in terminal. That means you can always start
:term and start using Git command-line interface like you're used to.
You can also start a specific command like
:term git status or
:term git add % (where
% refers to the current file path).
For simple commands, even simple bang! command
:help :!) would do. We can run any shell command like
:!git status or
:!git checkout %.
All those are excellent options, but we can do better.
The Gitgutter's primary purpose is to show signs in the gutter on the left of what lines have been changed, added, or modified. Here's how it looks.
See those little pluses and minuses on the left. That's it.
While it's on its own quite a nice addition, it also comes with more useful stuff.
You can jump between "hunks" (modified pieces of code) with
While at a chunk, you either add/stage it with
<leader>hs, or revert/undo it with
To preview the change, press
<leader>hp. That's handy when you need to figure out what has changed.
And it's not all, so check it out.
Tim Pope is the guy behind so many awesome Vim plugins, and this one is his most popular.
It's "so awesome, it should be illegal," as we all agree.
Here's how I use it.
Git without any argument brings up the summary screen. From there, you can use mappings to add files (
-), and finally commit your changes(
= shows the current diff. There are many more mappings (press
g? to see them all).
Git blame shows a commit hash and author near each line of code. Press enter at any lineg, and it will take you to that commit's diff. I use it so often I have it aliased to
You also do diffing, merging, and all the kinds of crazy stuff with fugitive. Have a look at the docs!
Sometimes we need to open a file in a browser (using Github UI), look around, or send a link to a colleague.
With this plugin installed, it can be done by running
:Gbrowse either in normal or a visual mode:
- in normal mode, it will open the entire file in the web browser
- in visual mode, it will open the file with the selected lines highlighted (with Github, it works by appending a hash to the file's URL, e.g.
This plugin by the FZF author adds a command
:GV, which brgings on the Git commit log. Each commit can be viewed right in a split which is very handy.
As you can see, there are many great plugins that will make your life with Git much easier.
There are, of course, others, which you may want to check out as well (though I don't use them, so I can't say if they are any good).