flotzilla
flotzilla

Reputation: 1258

vimdiff: force line-by-line comparison (ignore supposedly missing/additional lines)

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

Upvotes: 17

Views: 9553

Answers (3)

jas_raj
jas_raj

Reputation: 1161

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

In bash you can add this to your .bashrc so you can use linediff from the command line to just normally call a diff between two files with the above:

linediff() { 
     if [ -z "$1" ] || [ -z "$2" ]; then return; fi
     f1=$(basename "$1")
     f2=$(basename "$2")
     cat -n "$1" > "/tmp/$f1"
     cat -n "$2" > "/tmp/$f2"
     vimdiff "/tmp/$f1" "/tmp/$f2"
     rm "/tmp/$f1" "/tmp/$f2"
 }

and now linediff file1 file2 will do the above and clean up after.

Upvotes: 13

Rick Howe
Rick Howe

Reputation: 449

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.

Upvotes: 4

Ingo Karkat
Ingo Karkat

Reputation: 172758

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.

Upvotes: 4

Related Questions