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

做~自己de王妃 提交于 2019-12-04 01:01:31

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!