Using Vim in Git for Windows

· jollygood's blog

#git #vim

This post describes how to configure Git for Windows to use the system Vim as default editor and merge tool. It took some time for me to figure out the small details so I'm documenting this for reference.

Ruslan Osipov has written a great blog post that describes using vimdiff as git mergetool. The git config in that post works for Linux, but for Windows we need some additional configuration.

I use Git in Windows mostly from PowerShell and there Git loads its own builtin Vim by default, which doesn't use my Vim configuration in Windows. As an example of why this is a problem, see the colors in the following screenshots when using 'vimdiff' as mergetool. First: Git Vim - the color contrasts in the lines under '<<<<<<< HEAD' makes the test unreadable:

Git builtin vimdiff

Second: System Vim - my Vim configuration loads colors that are readable:

System vimdiff

Here is how to configure system Vim for Git in Windows together with some lessons learned while figuring this out.

# How to configure system Vim as editor in Windows

  1. Run this command in PowerShell:
> git config --global core.editor 'C:\Program Files (x86)\Vim\vim90\vim.exe'
  1. This adds the following line to ~/.gitconfig:
    editor = C:\\Program Files (x86)\\Vim\\vim90\\vim.exe
  1. Edit ~/.gitconfig and add quotes around the path:
    editor = 'C:\\Program Files (x86)\\Vim\\vim90\\vim.exe'

# Lessons learned

If I try using it without adding the quotes, I get an error:

[...] error: There was a problem with the editor 'C:\Program Files (x86)\Vim\vim90\vim.exe'. [...]

If I run any of the following, I get Git's builtin Vim:

> git config --global core.editor vim
> git config --global core.editor vim.exe

As an alternative: if I run the following I get the system Vim. At least when I've installed Vim with bat files:

> git config --global core.editor vim.bat

# How to configure system Vim as merge tool

In PowerShell run the following:

> git config --global mergetool.vimdiff.path 'C:\Program Files (x86)\Vim\vim90\vim.exe'
> git config --global mergetool.gvimdiff.path 'C:\Program Files (x86)\Vim\vim90\gvim.exe'

Now we can do three-way merging in system Vim or GVim like so:

> git mergetool -t vimdiff
> git mergetool -t gvimdiff

To set the default merge tool:

> git config --global merge.tool vimdiff

To use the default merge tool:

> git mergetool

# Lessons learned

We only need to change mergetool.vimdiff.path. Leave mergetool.vimdiff.cmd untouched.

We don't need to add additional quotes to the mergetool config, as we had to do for the editor configuration.

# A note on fugitive.vim for merging

On a side note, the fugitive.vim Git plugin for Vim is superb. I use it every day. However, I prefer the three-way merge layout in Git's vimdiff over the layout in fugitive.vim.

fugitive.vim provides three-way merging with the :Gdiffsplit! command. It has a LOCAL/MERGED/REMOTE layout without BASE, like so:

+----------------------------------------+
|             |           |              |
|             |           |              |
|   LOCAL     |   MERGED  |   REMOTE     |
|             |           |              |
|             |           |              |
+----------------------------------------+

I prefer Git's default vimdiff layout which includes the BASE window, like the KDiff3 three-way merge layout although in different order:

+-------------+-----------+--------------+
|             |           |              |
|   LOCAL     |   BASE    |   REMOTE     |
|             |           |              |
+-------------+-----------+--------------+
|                                        |
|                MERGED                  |
|                                        |
+----------------------------------------+

# Why use vimdiff?

KDiff3 was my go-to merge tool for many years and I can still recommend it.

However, as a long-term Vim user, I've gravitated more and more to resolving merge conflicts with Vim. So far mostly manually resolving conflicts by simply editing the conflicting hunks. Using vimdiff as a three-way merge tool is simply the next step on my Vim journey.

# References