15

Is there any way to set vimdiff's diff strategy to be the patience algorithm? It's built into git and seems to be much better than a normal diff.

For reference:

0

3 Answers 3

14

As of vim 8.1.0360 (Sep 2018), vim (except as packaged by Apple 😠) ships with xdiff (the same library that git uses for diffs), meaning patience diff is now natively supported in vim and neovim (see neovim issue 1466). Add this to your vimrc:

if has("mac") && $VIM == "/usr/share/vim" set diffopt-=internal elseif has("patch-8.1.0360") set diffopt+=internal,algorithm:patience endif 

A nice introduction to both the new algorithm:patience and indent-heuristic (added in the same release) diff options can be found at Vimways ~ The power of diff.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it ironic that I found this post because I was impatient and searched for "change vimdiff algorithm"? Huh, Ms. Morrissette?
I think your example is missing the indent-heuristic flag.
@Luke – indent-heuristic is not directly related to the patience algorithm and therefore isn't in my example settings for the patience algorithm. It is, however, in my .vimrc 😉
Also note that stock vim on macOS does not support xdiff despite having the patch.
Oof! Detecting it is quite difficult because, as @ChristianBrabandt noted, "we could of course add an additional version flag, that checks whether xdiff has been compiled in. That would allow to easier check this. But I'd rather see Apple not removing the xdiff part obviously" ... so the only solution is to assume a Mac lacks xdiff support. The linked solution further controls this by applying this assumption only when vim is in /usr/share. That sounds as good as we can get, so I'll replicate that here.
8

I made a plugin that allows this. Try my EnhancedDiff plugin

Comments

3

see :help diff-diffexpr: http://vimdoc.sourceforge.net/htmldoc/diff.html#diff-diffexpr

you might be able to set it to something like

set diffexpr=MyDiff() function MyDiff() let opt = "" if &diffopt =~ "iwhite" let opt = opt . "-w " endif silent execute "!git diff --no-index --patience " . opt . v:fname_in . " " . v:fname_new . " > " . v:fname_out endfunction 

I tried this, but I did not get it to work as git outputs unified diff format, while vim expects ed style format (see doc above). You might have to transform the output of git diff, which is probably not what you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.