有没有办法配置 vimdiff 以忽略所有空格?

发布于 2024-08-01 19:54:38 字数 384 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞旧 2024-08-08 19:54:38

更新:补丁 8.1.0393 起 (2018-09 -15), diffopt 可以被告知忽略所有空白:

set diffopt+=iwhiteall

如果你想要完全向后兼容,请像这样实现:

if exists('&diffopt')
  if has("patch-8.1.0393")
    set diffopt+=iwhiteall
  else
    set diffopt+=iwhite
    function DiffW()
      let opt = ""
       if &diffopt =~ "icase"
         let opt = opt . "-i "
       endif
       if &diffopt =~ "iwhite"
         let opt = opt . "-w "
       endif
       silent execute "!diff -a --binary " . opt .
         \ v:fname_in . " " . v:fname_new .  " > " . v:fname_out
       redraw
    endfunction
    set diffexpr=DiffW()
  endif
endif

我从 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:

set diffopt+=iwhiteall

If you want full backwards compatibility, implement like this:

if exists('&diffopt')
  if has("patch-8.1.0393")
    set diffopt+=iwhiteall
  else
    set diffopt+=iwhite
    function DiffW()
      let opt = ""
       if &diffopt =~ "icase"
         let opt = opt . "-i "
       endif
       if &diffopt =~ "iwhite"
         let opt = opt . "-w "
       endif
       silent execute "!diff -a --binary " . opt .
         \ v:fname_in . " " . v:fname_new .  " > " . v:fname_out
       redraw
    endfunction
    set diffexpr=DiffW()
  endif
endif

I borrowed this function from the diffexpr docs, changing -b to -w and adding a redraw 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.

几度春秋 2024-08-08 19:54:38

是的。 像您一样设置 iwhite 选项,但另外,将 diffexpr 设为空。

来自 vim 文档 的相关部分:

忽略空白量的变化。 添加
“diff”命令的“-b”标志如果
“diffexpr”为空。 检查文档
“diff”命令的作用
确切地。 它应该忽略添加尾随
空格,但不包括前导空格。

另请注意,您可以通过设置 diffexpr 来提供自定义 diff 命令行。 请参阅 vimdiff 手册页 上的讨论,特别是:

“diffexpr”选项可以设置为使用标准以外的其他内容
“diff”程序比较两个文件并找出差异。

当 'diffexpr' 为空时,Vim 使用此命令查找差异
文件1和文件2之间:

diff file1 file2 >   输出文件 
  

Yes. Set the iwhite option as you did, but additionally, make diffexpr empty.

From the relevant section of the vim docs:

iwhite

Ignore changes in amount of white space. Adds
the "-b" flag to the "diff" command if
'diffexpr' is empty. Check the documentation
of the "diff" command for what this does
exactly. It should ignore adding trailing
white space, but not leading white space.

Note also that you can provide a custom diff command line by setting diffexpr. See the discussion on the vimdiff man page, in particular:

The 'diffexpr' option can be set to use something else than the standard
"diff" program to compare two files and find the differences.

When 'diffexpr' is empty, Vim uses this command to find the differences
between file1 and file2:

diff file1 file2 > outfile
羅雙樹 2024-08-08 19:54:38

谢谢我,这对我有帮助。 我现在只需要在我的 ~/.vimrc 中添加这个(比 Adam K 提出的更简单):

set diffopt+=iwhite

set diffexpr=""

它就可以了...这仍然是我所知道的最强大的 diff 工具,比任何其他工具都要好。

Thanks ire, that helped me. I now only need to have this (simpler than what is proposed by Adam K) in my ~/.vimrc :

set diffopt+=iwhite

set diffexpr=""

And it does it... That is still the most powerfull diff tool I know of, far better than any other.

梦亿 2024-08-08 19:54:38

我知道这是一个古老的问题,但对于像我这样不知道的人来说,现在可以使用:

: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'

遮了一弯 2024-08-08 19:54:38

对于那些执行 set diffopt+=iwhite 时遇到“无效参数”的人,请尝试不使用 +,如下所示:

set diffopt=iwhite

但是,更可靠的方法是设置忽略空白,同时保留现有选项。 但请注意,“无效参数”错误可能是由不支持的现有选项之一引起的。 就我而言,它是“内部”选项,因此我需要按以下顺序设置选项:

set diffopt-=internal
set diffopt+=iwhite

或者将以下内容添加到您的 .vimrc 中:

if &diff
    set diffopt-=internal
    set diffopt+=iwhite
endif

归功于 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:

set diffopt=iwhite

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:

set diffopt-=internal
set diffopt+=iwhite

Or add the following to your .vimrc:

if &diff
    set diffopt-=internal
    set diffopt+=iwhite
endif

Credit to https://www.micahsmith.com/blog/2019/11/fixing-vim-invalid-argument-diffopt-iwhite/

橘味果▽酱 2024-08-08 19:54:38

解决 Adam Katz 解决方案的评论中提出的问题:

根据 vim 版本和用户设置,silent 命令可能会在发出后忽略重绘屏幕。 我也遇到了这个问题,每当我在使用建议的 diffexpr 后执行 :diffo 时就会出现这个问题。 我的解决方案是将静默执行命令更改为以下内容:

silent execute "!diff -a --binary " . opt .
 \ v:fname_in . " " . v:fname_new .  " > " . v:fname_out | redraw!

这会在发出命令后强制重绘。

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 suggested diffexpr. My solution was to change the silent execute command to the following:

silent execute "!diff -a --binary " . opt .
 \ v:fname_in . " " . v:fname_new .  " > " . v:fname_out | redraw!

This forces a redraw after the command is issued.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文