251

I setup git diff to wrap into vimdiff, using "Git Diff with Vimdiff" as a guide, and it's working as expected unless there are many files with changes.

When there are multiple files with changes and I run git diff, it opens the first file and, after quitting the first instance of vimdiff, I'm presented with the following message:

external diff died, stopping at filename

This is a completely different behavior than I am used to. I had a similar setup in the past with SVN and, when diffing against multiple files, I would review the first file, then write and quit using :wq and the next file with differences would open up.

This is not the case with Git. I tried :n[ext], but doing so does not fill the left window with the original file so that it can be diffed against the modified version.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
chuckg
  • 9,195
  • 7
  • 28
  • 26

5 Answers5

399
git config --global diff.tool vimdiff
git config --global difftool.prompt false

Typing git difftool yields the expected behavior.

Navigation commands,

  • :qa in vim cycles to the next file in the changeset without saving anything.

Aliasing (example)

git config --global alias.d difftool

.. will let you type git d to invoke vimdiff.

Advanced use-cases,

  • By default, git calls vimdiff with the -R option. You can override it with git config --global difftool.vimdiff.cmd 'vimdiff "$LOCAL" "$REMOTE"'. That will open vimdiff in writeable mode which allows edits while diffing.
  • :wq in vim cycles to the next file in the changeset with changes saved.
Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47
chuckg
  • 9,195
  • 7
  • 28
  • 26
  • 24
    Just my two cents: I have `df` aliased to `diff` and `dt` aliased to `difftool`. Also, typing `:qa` in Vim will cycle to the next changeset without saving anything. – J.C. Yamokoski Nov 26 '12 at 21:56
  • 2
    This solution is great except that when saving changes you have to type ":w!" instead of :w – Asenar Sep 26 '13 at 09:30
  • 2
    @jonyamo Setting alias should be always based on how often we are using some commands. I am often using `git diff` than `git difftool`. So I have aliased `d` to 'diff' and `'dt' to `difftool'. Usability is matter than creating aliases with pattern. – Mohammed H Jan 08 '14 at 05:14
  • How to make "git diff" use vimdiff?, this answer makes me use an alias. If I alias to "diff" doesn't work. – Rombus May 04 '16 at 02:52
  • 9
    @Asenar _"This solution is great except that when saving changes you have to type `:w!` instead of `:w`"._ That is because git calls `vimdiff` with the `-R` option. You can override it with `git config --global difftool.vimdiff.cmd 'vimdiff "$LOCAL" "$REMOTE"'`. That will open vimdiff in writeable mode. – wisbucky Apr 26 '18 at 18:37
  • It's also handy to put `map :qa!` in your .vimrc, then just hold down F4 if you are finished with your vimdiff, because git `vimdiff` will open every file comparison one after another. – ericcurtin Aug 20 '19 at 09:50
  • 2
    Moved some of these upvoted comments into the answer. Thanks. – Shyam Habarakada Dec 17 '20 at 20:39
  • For anyone wanting to set other tools: `git difftool --tool-help` shows the list. – Nader Ghanbari May 16 '21 at 03:02
  • NOTE: the `difftool.prompt false` is there because otherwise git will not open the tool directly (i think it prompts the user first) https://stackoverflow.com/questions/7897517/why-does-git-difftool-not-open-the-tool-directly – Trevor Boyd Smith Aug 19 '21 at 12:52
111

You can try git difftool, it is designed to do this stuff.

First, you need to config diff tool to vimdiff

git config diff.tool vimdiff

Then, when you want to diff, just use git difftool instead of git diff. It will work as you expect.

Undo
  • 25,519
  • 37
  • 106
  • 129
czchen
  • 5,850
  • 2
  • 26
  • 17
  • 1
    Almost there! Modified a bit it's exactly what I'm looking for. +1 though! – chuckg Sep 15 '10 at 00:27
  • 1
    The vimdiff mergetool has recently been updated: http://git.kernel.org/?p=git/git.git;a=commit;h=829ef383a2b03a614d7d23e575270f2b10a805c1 (and a few other commits, but that's the biggest one). Not sure when the next maintenance release will be, but if you're willing to build from git.git, upgrades shall be yours! – Cascabel Oct 29 '10 at 05:59
24
Git accepts kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge,
and opendiff as valid diff tools. You can also set up a custom tool. 

git config --global diff.tool vimdiff
git config --global diff.tool kdiff3
git config --global diff.tool meld
git config --global diff.tool xxdiff
git config --global diff.tool emerge
git config --global diff.tool gvimdiff
git config --global diff.tool ecmerge
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
  • 3
    You answer for the mergetool, not the difftool. But it is still a helpful answer. +1. – dotancohen Aug 21 '13 at 12:41
  • @dotancohen Really Thanks i have never notice that on silly mistake now i can edit my answer. – Nanhe Kumar Aug 21 '13 at 20:17
  • 1
    @NanheKumar, @dotancohen, replacing `merge` with `diff` in the above answers will do the trick, *i.e.* `git config --global diff.tool vimdiff`. – Will Sep 03 '13 at 14:56
11

If you want to permanently use vimdiff for git diff, you can set the ~/.gitconfig file:

git config --global diff.tool vimdiff

and then you can use git difftool to diff.

If you only want to temporarily use vimdiff, you can run the command every time:

git difftool --tool=vimdiff
Victor
  • 1,303
  • 1
  • 10
  • 28
6

For people who want to use another diff tool not listed in git, say with nvim. here is what I ended up using:

git config --global alias.d difftool -x <tool name>

In my case, I set <tool name> to nvim -d and invoke the diff command with

git d <file>
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
salty-cat-fish
  • 111
  • 1
  • 3