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

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.


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.


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.

Tags:

Vim

Diff

Vimdiff