Using Git From Vim
Tags: vim, git, tooling
Published: 2021-09-17 15:59:53

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.

```bash
export EDITOR=vim
```

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

```bash
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](https://vimhelp.org/terminal.txt.html). 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](https://github.com/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unoqg6cgb9uwn4a55ess.png)
 
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](https://github.com/airblade/vim-gitgutter).


## [tpope/vim-fugitive](https://github.com/tpope/vim-fugitive)

[Tim Pope](https://twitter.com/tpope) 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`.

![fugitive_blame](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngz2fojrzz0wkcvlnj3i.png)

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

## [tpope/vim-rhubarb](https://github.com/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](https://github.com/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.

![gv](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgic55z96gliw696iq88.png) 

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

- [mhinz/vim-signify](https://github.com/mhinz/vim-signify) highlights the changes similarly to git-gutter
- [lambdalisue/gina.vim](https://github.com/lambdalisue/gina.vim) another way to create/update commits
- [sodapopcan/vim-twiggy](https://github.com/sodapopcan/vim-twiggy) a fugitive extension for managing branches
- [jreybert/vimagit](https://github.com/jreybert/vimagit) a full-blown alternative to fugitive