How can I use vim to view git diffs on Windows?

I want to use vim to view the output of git diff.

I am running Windows 8 and I have vim 7.4 installed. I have, for convenience, created an environment variable VIMDIR set to Vim's install directory (set VIMDIR=C:\Program Files (x86)\Vim\vim74), and added this directory to the %PATH%.

Vim comes with a %VIMDIR%\macros\less.bat script, which can be used as a pager, so that less.bat file.txt or dir | less.bat will display the output in a vim-based pager. I use this regularly, and it works fine. I'd also like to use vim, rather than Git's less.exe, as git's pager, so that it runs when I do git diff, but I'm running into problems.

I have tried using vim's less.bat batch file

If I set %GIT_PAGER% to %VIMDIR%\macros\less.bat and do a git diff, I get an error message:

C:\Program Files (x86)\Vim\vim74\macros\less.bat: -c: line 0: syntax error near unexpected token `('
C:\Program Files (x86)\Vim\vim74\macros\less.bat: -c: line 0: `C:\Program Files (x86)\Vim\vim74\macros\less.bat'

I believe this is because git is an MSYS program, meaning it can't handle Windows-style C:\whatever paths.

I have also tried invoking vim's less macro directly

So that doesn't work. The next thing I tried was to examine less.bat and set %GIT_PAGER% to something based on that file. In that file, I see this line, which instructs vim to read from stdin:

vim --cmd "let no_plugin_maps = 1" -c "runtime! macros/less.vim" -

I set %GIT_PAGER% to a modified version of this that uses MSYS-style paths:

set GIT_PAGER="/c/Program Files (x86)/Vim/vim74/vim.exe" --cmd "let no_plugin_maps = 1" -c "runtime! macros/less.vim" -

That does successfully launch vim! But there is still a problem: colors aren't working and there are escape characters littered all over the screen. Note that normally, syntax highlighting works just fine. If I run these commands, it will display the diff file I generated with color:

git diff > test.diff
less.bat test.diff

However, if I simply call git diff and have it use %GIT_PAGER% to display the diff for me in the console, it shows me this:

screenshot

What else can I try?

I'm not sure what else to try. Is anyone else using vim to view git diffs on Windows? How are you doing it?

Thanks for any help.


The remaining problem is that git diff still highlights the diff output with colors (as ANSI escape sequences), and expects the pager (i.e. Vim) to display that correctly. But Vim has its own syntax highlighting (also for diffs), and doesn't understand the sequences, so they show up as ugly ^[[m. You have two options:

  1. Disable Git's diff highlighting (command-line argument --no-color, but you can also unconfigure that via git config). Now you need to ensure that Vim uses the correct filetype (:set filetype=diff; you can pass that on the command-line via -c {cmd}).

  2. Make Vim understand and highlight the ANSI escape sequences; the AnsiEsc.vim plugin can do this.

I'd recommend the first approach.