Is there a way to configure vimdiff to ignore ALL whitespaces?

家住魔仙堡 提交于 2019-11-28 17:09:10

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

This implements what you want (taken from the diffexpr docs with -b changed to -w):

set diffopt+=iwhite
set diffexpr=DiffW()
function DiffW()
  let opt = ""
   if &diffopt =~ "icase"
     let opt = opt . "-i "
   endif
   if &diffopt =~ "iwhite"
     let opt = opt . "-w " " swapped vim's -b with -w
   endif
   silent execute "!diff -a --binary " . opt .
     \ v:fname_in . " " . v:fname_new .  " > " . v:fname_out
endfunction

... I'm still looking for a better diffexpr helper with respect to handling which lines map to which (GNU diff, even with -w instead of -b, is rather baffled by combining extra whitespace with minor edits like commented lines). Maybe diffchar?

greg

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.

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.

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