如何强制vimdiff始终逐行比较两个文件而不识别添加或删除的行?
问题是,如果两个文件之间的差异很大,但是文件中的两行偶然匹配,vimdiff认为这些行是相同的,只是将其余行视为添加或删除的行,并且生成的diff完全不可用.在我的例子中,file1中的第i行始终对应于file2中的第i行,因此vimdiff没有添加或删除行的业务查找.
以下是一个小例子,其中两个文件包含两个变量的值,每个变量三次.Vimdiff错误地将file1/line1与file2/line3匹配,并认为已经添加或删除了它周围的一些行.差异(减去颜色)然后看起来像这样:
| 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
Run Code Online (Sandbox Code Playgroud)
但是,我想要的是以下内容,除了匹配的行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
Run Code Online (Sandbox Code Playgroud)
小智 5
在复制此示例进行尝试时,我注意到,vimdiff如果您具有与每一行关联的行号,那么它将完成您想要的操作。
因此,您可以使用cat添加行号,然后添加diff:
cat -n file1 > file1_with_line_no
cat -n file2 > file2_with_line_no
vimdiff file1_with_line_no file2_with_line_no
Run Code Online (Sandbox Code Playgroud)
然后根据需要输出(显示为,diff以方便复制到此处):
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
Run Code Online (Sandbox Code Playgroud)
小智 4
使用diffchar.vim插件怎么样?它在非差异模式下逐行比较。请在 2 个窗口中打开 2 个文件,然后按 F7。默认情况下,它会尝试按行中的字符查找差异,但您可以更改差异单位、单词或其他内容。