Viewing all `git diffs` with vimdiff
Asked Answered
T

5

255

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.

Thigmotaxis answered 14/9, 2010 at 23:54 Comment(0)
T
404
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.
Thigmotaxis answered 15/9, 2010 at 0:27 Comment(9)
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.Kurus
This solution is great except that when saving changes you have to type ":w!" instead of :wAllo
@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.Coomb
How to make "git diff" use vimdiff?, this answer makes me use an alias. If I alias to "diff" doesn't work.Ampulla
@Allo "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.Dulsea
It's also handy to put map <F4> :qa!<CR> 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.Insectivore
Moved some of these upvoted comments into the answer. Thanks.Maori
For anyone wanting to set other tools: git difftool --tool-help shows the list.Newtonnext
NOTE: the difftool.prompt false is there because otherwise git will not open the tool directly (i think it prompts the user first) stackoverflow.com/questions/7897517/…Jesselton
F
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.

Fiann answered 15/9, 2010 at 0:8 Comment(2)
Almost there! Modified a bit it's exactly what I'm looking for. +1 though!Thigmotaxis
The vimdiff mergetool has recently been updated: git.kernel.org/?p=git/… (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!Sinistrorse
G
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
Guttersnipe answered 9/7, 2013 at 6:5 Comment(3)
You answer for the mergetool, not the difftool. But it is still a helpful answer. +1.Alcina
@Alcina Really Thanks i have never notice that on silly mistake now i can edit my answer.Guttersnipe
@NanheKumar, @dotancohen, replacing merge with diff in the above answers will do the trick, i.e. git config --global diff.tool vimdiff.Roley
A
15

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
Acclamation answered 13/5, 2021 at 11:24 Comment(1)
I was indeed looking for a temporary solution. Thank you for that last addition.Fez
R
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>
Riven answered 19/4, 2020 at 4:11 Comment(1)
nvimdiff is now supported as a tool so you could do git config --global diff.tool nvimdiff.Newtonnext

© 2022 - 2024 — McMap. All rights reserved.