⭐ Vim-based IDE? Check out my Vim For Developers book

Using Git From Vim

August 24, 2021

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.

Commit message

You probably want to edit your commit messages in Vim.

To do it, add the EDITOR environment variable.

Put this into your .bashrc / .zshrc file.

export EDITOR=vim

As an alternative, set the editor in ~/.gitconfig (this option has a priority over $EDITOR):

git config --global core.editor nvim

The 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 :! (see :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.

airblade/vim-gitgutter

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.

GitGutter
GitGutter

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 ]c / ]c.

  • While at a chunk, you either add/stage it with <leader>hs, or revert/undo it with <leader>hu.

  • 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.

tpope/vim-fugitive

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 (s), unstage(u), toggle(-), and finally commit your changes(cc). = 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 <leader>a.

Git blame in action
Git blame in action

You also do diffing, merging, and all the kinds of crazy stuff with fugitive. Have a look at the docs!

tpope/vim-rhubarb

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. #L61-L62)

junegunn/gv.vim

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.

My commit history as viewed in GV
My commit history as viewed in GV

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).

Vim For Developers

Vim For Developers

Learn Vim and upgrade your productivity to the next level by building the IDE of your dreams.

LEARN MORE