How do I force vimdiff to always compare two files line-by-line without identifying added or deleted lines?
The problem is that if the diff between two files is large, but by chance two lines in the file match up, vimdiff thinks these lines are the same and just treats the rest as added or deleted lines, and the resulting diff is totally unusable. In my case, line i in file1 always corresponds to line i in file2, so vimdiff has no business finding added or deleted lines.
Following is a small example with two files containing the values of two variables three times each. Vimdiff erroneously matches up file1/line1 with file2/line3 and thinks some lines around it have been added or deleted. The diff (minus colors) then looks like this:
| 1 foo 8.1047 < del/new
| 2 bar 6.2343 < del/new
1 foo 0.0000 | 3 foo 0.0000 < match
2 bar 5.3124 | 4 bar 1.4452 < wrong
3 foo 4.5621 | < new/del
4 bar 6.3914 | < new/del
5 foo 1.0000 | 5 foo 1.0000 < match
6 bar 6.3212 | 6 bar 7.2321 < wrong
What I want, however, is the following, with all lines marked as wrong except for the matching lines 5:
1 foo 0.0000 | 1 foo 8.1047 < wrong
2 bar 5.3124 | 2 bar 6.2343 < wrong
3 foo 4.5621 | 3 foo 0.0000 < wrong
4 bar 6.3914 | 4 bar 1.4452 < wrong
5 foo 1.0000 | 5 foo 1.0000 < match
6 bar 6.3212 | 6 bar 7.2321 < wrong
How about using diffchar.vim plugin? It compares line-by-line in non-diff mode. Please open 2 files on 2 windows and then just press F7. By default, it tries to find the differences by characters in a line, but you can change the difference units, words or something.
As I was copying this example to try it, I noticed that vimdiff
will do what you want if you have the line number associated with each line.
Therefore, you can use cat
to add the line number and then diff:
cat -n file1 > file1_with_line_no
cat -n file2 > file2_with_line_no
vimdiff file1_with_line_no file2_with_line_no
The output is then as you want (shown with diff
for easy copying to here):
diff file1_with_line_no file2_with_line_no --side-by-side
1 foo 0.0000 | 1 foo 8.1047
2 bar 5.3124 | 2 bar 6.2343
3 foo 4.5621 | 3 foo 0.0000
4 bar 6.3914 | 4 bar 1.4452
5 foo 1.0000 5 foo 1.0000
6 bar 6.3212 | 6 bar 7.2321
Vim relies on the external diff
command to analyze the two files, so you can influence the result via a different tool that uses a different algorithm. You can configure that via the 'diffexpr'
option; the tool's output has to be in "ed" style. Cp. :help diff-diffexpr
.
Note that this only affects the lines added / changed / deleted; for displaying the character differences in a changed line itself, Vim does that on its own.
Unfortunately, I don't know any alternative diff tool that could provide such output, but maybe others can fill in that.
来源:https://stackoverflow.com/questions/27781235/vimdiff-force-line-by-line-comparison-ignore-supposedly-missing-additional-lin