有没有办法配置 vimdiff 以忽略所有空格?
我使用 vim -d file1 file2 来查看它们之间的差异。 这工作正常,但我想忽略空白更改 - 它们与源代码文件无关。
Vim 帮助指出以下命令将发挥作用:
set diffopt+=iwhite
但不幸的是,该命令仅将 -b
添加到 diff 工具命令行,并且仅忽略尾随空格。 diff 的正确命令行键应该是 -w
,以忽略所有空白更改。 但我找不到如何直接从 Vim 修改 diff 命令行。 当然,我可以编译自定义 diff,或者用 diff.sh 替换 diff,但这看起来有点难看:(。
有没有更好的方法来修改 Vim 与 diff 工具交互以显示文件差异的方式?
I'm using vim -d file1 file2
in order to see the differences between them. This works fine, but I want to ignore whitespace changes - they are irrelevant for source code files.
Vim help states that the following command will do the magic:
set diffopt+=iwhite
But unfortunately, this command only adds -b
to diff tool command line, and that only ignores trailing whitespaces. The correct command line key for diff should be -w
, to ignore all whitespace changes. But I can't find how to modify the diff command line directly from Vim. Of course I can compile a custom diff, or replace diff with diff.sh, but that looks kinda ugly :(.
Is there a better way to modify how Vim interacts with the diff tool for displaying file differences?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新: 自补丁 8.1.0393 起 (2018-09 -15),
diffopt
可以被告知忽略所有空白:如果你想要完全向后兼容,请像这样实现:
我从
diffexpr
文档,将-b
更改为-w< /code> 并添加
redraw
命令以确保屏幕立即重新绘制,而不是等待用户按 Enter。在我改进 vim diff 功能的相关任务中,我发现 patience diff 支持< /a> 我一直想玩弄 diffchar 插件。
Update: As of patch 8.1.0393 (2018-09-15),
diffopt
can be told to ignore all white space:If you want full backwards compatibility, implement like this:
I borrowed this function from the
diffexpr
docs, changing-b
to-w
and adding aredraw
command to ensure the screen repaints immediately rather than waiting for the user to hit Enter.In my related quests to improve vim's diff functionality, I've found patience diff support and I've been meaning to toy with the diffchar plugin.
是的。 像您一样设置
iwhite
选项,但另外,将diffexpr
设为空。来自 vim 文档 的相关部分:
另请注意,您可以通过设置
diffexpr
来提供自定义 diff 命令行。 请参阅 vimdiff 手册页 上的讨论,特别是:Yes. Set the
iwhite
option as you did, but additionally, makediffexpr
empty.From the relevant section of the vim docs:
Note also that you can provide a custom diff command line by setting
diffexpr
. See the discussion on the vimdiff man page, in particular:谢谢我,这对我有帮助。 我现在只需要在我的 ~/.vimrc 中添加这个(比 Adam K 提出的更简单):
它就可以了...这仍然是我所知道的最强大的 diff 工具,比任何其他工具都要好。
Thanks ire, that helped me. I now only need to have this (simpler than what is proposed by Adam K) in my ~/.vimrc :
And it does it... That is still the most powerfull diff tool I know of, far better than any other.
我知道这是一个古老的问题,但对于像我这样不知道的人来说,现在可以使用:
:set diffopt+=iwhiteall
将“-w”标志添加到“diff”命令 if 'diffexpr ' 是空的。
请参见
:h 'diffopt'
I know it's an antique question but for others like me who didn't know, this is now available:
:set diffopt+=iwhiteall
Adds the "-w" flag to the "diff" command if 'diffexpr' is empty.
See
:h 'diffopt'
对于那些执行
set diffopt+=iwhite
时遇到“无效参数”的人,请尝试不使用+
,如下所示:但是,更可靠的方法是设置忽略空白,同时保留现有选项。 但请注意,“无效参数”错误可能是由不支持的现有选项之一引起的。 就我而言,它是“内部”选项,因此我需要按以下顺序设置选项:
或者将以下内容添加到您的 .vimrc 中:
归功于 https://www.micahsmith.com/blog/2019/11/fixing-vim-invalid-argument-diffopt-iwhite /
For those hitting "Invalid argument" doing
set diffopt+=iwhite
, try without the+
like so:However, a more robust approach would be to set ignore whitespace while preserving existing options. Beware though, that the "Invalid argument" error is likely caused by one of those existing options not being supported. In my case it was the "internal" option therefore I needed to set options in the following order:
Or add the following to your .vimrc:
Credit to https://www.micahsmith.com/blog/2019/11/fixing-vim-invalid-argument-diffopt-iwhite/
解决 Adam Katz 解决方案的评论中提出的问题:
根据 vim 版本和用户设置,
silent
命令可能会在发出后忽略重绘屏幕。 我也遇到了这个问题,每当我在使用建议的diffexpr
后执行:diffo
时就会出现这个问题。 我的解决方案是将静默执行命令更改为以下内容:这会在发出命令后强制重绘。
Addressing an issue brought up in the comments of Adam Katz's solution:
Depending on the vim version and setup of the user, a
silent
command can neglect to redraw the screen after it is issued. I also encountered this problem, which arose whenever I executed:diffo
after using the suggesteddiffexpr
. My solution was to change the silent execute command to the following:This forces a redraw after the command is issued.