Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The way to improve vimdiff similarity-searching mechanism

Tags:

vim

I'm trying to understand how vimdiff work.

Here I tried to diff two simple files. The 1st:

abcdefghijklmnopqrstuvwxyz

foo

abcdefghijklmnopqrstuvwxyz iii

bar

The 2nd:

abcdefghijklmnopqrstuvwxyz

abcdefghijklmnopqrstuvwxyz

Here is result from diffmerge utility:

enter image description here

And here is result from vim:

enter image description here

Note that vim doesn't see the similarity between abc...xyz and abc...xyz iii lines and doesn't arrange them visually on one line.

Is there some settings to improve vimdiff in such cases?

like image 750
user90726 Avatar asked Apr 09 '16 16:04

user90726


1 Answers

As pointed out in the comments, vim uses the external utility diff which doesn't do word diffs nor can it find moved-and-modified lines.

Even resorting to diffexpr is unsatisfactory, since Vim expects the data from the external utility to be in a "ed" style diff. This rules out using alternative diff utilities that make word or even char based diffs, like diffmerge.

The workaround some industrious plugin authors have cooked up is to wrap the results of these more sophisticated diffs and translate their output into the "ed" style diff for Vim to consume. I know of two such plugins which take this approach: chardiff and vim-diff-enhanced. Both give you word diffs. In addition, Vim-diff-enhanced allows you to toggle between various diff algorithms (myers, histogram, patience) so one may decide which diff produces the best result on a case by case basis.

I use vim-diff-enhanced, and it is better than the default diff but still not great. (note: it depends on git.) For example, I tried your test, as described in the question, and it did NOT recognize that lines 5 and 3 were the same (even with :set diffopt+=iwhite). Although, if I removed or added something to line 2 of your first sample file, then vimdiff matched the results you obtained from diffmerge.

like image 166
gregory Avatar answered Nov 14 '22 23:11

gregory