to vimdiff twofile, except of...

10 views
Skip to first unread message

meino....@gmx.de

unread,
Feb 9, 2011, 11:47:35 AM2/9/11
to vim...@googlegroups.com

Hi,

I often have to compare two files with nearly identical
contents.

There two two kinds of changes, though:
There are tags, which are named after the name of the file.
And there are "true changes", which i am interested in.

Is it possible to instruct vim that way to not to mark
all lines which only differ in the tag's name?

Thank you very much in advance for any help!

Best regards,
mcc

Ben Schmidt

unread,
Feb 9, 2011, 7:07:38 PM2/9/11
to vim...@googlegroups.com

It is not really Vim that needs to be instructed to do this, but diff.
Then you simply instruct Vim to pass the relevant option to diff using
diffexpr. On my system, diff supports an -I option which instructs it to
ignore changes to lines which match a particular pattern, so I would use
that, e.g.

set diffexpr=MyDiff()
function MyDiff()
let opt = "-I tagpattern"
if &diffopt =~ "icase"
let opt = opt . "-i "
endif
if &diffopt =~ "iwhite"
let opt = opt . "-b "
endif
silent execute "!diff -a --binary " .
\ opt . v:fname_in . " " . v:fname_new .
\ " > " . v:fname_out
endfunction

(Adapted from :help diff-diffexpr. It probably doesn't work with
filenames with spaces in them as it stands; it should have some
shellescape() calls in there or something, probably.)

HTH,

Ben.

meino....@gmx.de

unread,
Feb 9, 2011, 7:49:58 PM2/9/11
to vim...@googlegroups.com
Ben Schmidt <mail_ben...@yahoo.com.au> [11-02-10 01:12]:
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

Hi Ben,

thank you for the script and your help.

What can I do under WIN, where there is no diff?
Unfortunately I am not allowed to install cygwin on my
PC at work (or install anything)....

At home I will use your hint at once !

As I understand, now all lines are skipped, which contain
"tag" but what happens with those lines including a "real"
difference AND the "tag"?

Best regards,
mcc

Ben Fritz

unread,
Feb 9, 2011, 11:16:10 PM2/9/11
to vim_use


On Feb 9, 6:49 pm, meino.cra...@gmx.de wrote:
>
> What can I do under WIN, where there is no diff?
> Unfortunately I am not allowed to install cygwin on my
> PC at work (or install anything)....
>

If diff mode is working in Vim at all, you do have diff already. It is
probably the version of diff that ships with the Windows Vim. I do not
know if it supports the option you are looking for, why not try it?

meino....@gmx.de

unread,
Feb 9, 2011, 11:31:40 PM2/9/11
to vim_use
Ben Fritz <fritzo...@gmail.com> [11-02-10 05:20]:
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

Hi Ben,

...I ever thought, that there are two vimdiffs (intern + external)
as with grep.
I will check it out! Thank you :)

Best regards,
mcc

Ben Schmidt

unread,
Feb 10, 2011, 12:14:33 AM2/10/11
to vim...@googlegroups.com
>> It is not really Vim that needs to be instructed to do this, but
>> diff. Then you simply instruct Vim to pass the relevant option to
>> diff using diffexpr. On my system, diff supports an -I option which
>> instructs it to ignore changes to lines which match a particular
>> pattern, so I would use that, e.g.
>>
>> set diffexpr=MyDiff()
>> function MyDiff()
>> let opt = "-I tagpattern"
>> if&diffopt =~ "icase"

>> let opt = opt . "-i "
>> endif
>> if&diffopt =~ "iwhite"

>> let opt = opt . "-b "
>> endif
>> silent execute "!diff -a --binary " .
>> \ opt . v:fname_in . " " . v:fname_new .
>> \ "> " . v:fname_out
>> endfunction
>>
>> (Adapted from :help diff-diffexpr. It probably doesn't work with
>> filenames with spaces in them as it stands; it should have some
>> shellescape() calls in there or something, probably.)
>>
>
> As I understand, now all lines are skipped, which contain
> "tag" but what happens with those lines including a "real"
> difference AND the "tag"?

That's awkward.

I think, though, that Vim only parses the 'headers' of the diff output,
and doesn't actually look at the content. So one way you could deal with
this would be to remove all the tags before diffing them, and as long as
the line numbers don't change, it would still work. I think v:fname_in
and v:fname_new are always temporarly files when Vim uses 'diffexpr', so
you could even just alter these files in-place. (You'd better check
this, though!) Something like:

set diffexpr=MyDiff()
function MyDiff()
execute "split " . v:fname_in
s/tagpattern//
w
bwipeout
execute "split " . v:fname_new
s/tagpattern//
w
bwipeout


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

It might work. But, of course, it will break if any one of my
assumptions above is incorrect, and most particularly, it will be
dangerous if I'm wrong about 'diffexpr' always using temporary files. So
tread carefully.

Ben.

meino....@gmx.de

unread,
Feb 10, 2011, 7:28:38 PM2/10/11
to vim...@googlegroups.com
Ben Schmidt <mail_ben...@yahoo.com.au> [11-02-10 16:57]:

Hi Ben,

thank you very much for your help!
I will check that out -- it will help me a lot! :)

Have a nice weekend!
Best regards,
mcc

Keith Christian

unread,
Feb 11, 2011, 4:39:42 PM2/11/11
to vim...@googlegroups.com
On Wed, Feb 9, 2011 at 9:47 AM, <meino....@gmx.de> wrote:
>
> Hi,
>
> I often have to compare two files with nearly identical
> contents.

If you are unable to install additional comparison utilities and still
need to compare files in a detailed fashion, there are a few
possibilities.

1. Open a CMD.EXE window, use the "fc" command. Works best on ascii files.

In the example below, "7" is deleted from file two, and 11 is added to it.

fc /l /n one two

Comparing files one and TWO
***** one
6: 6
7: 7
8: 8
***** TWO
6: 6
7: 8
*****

***** one
***** TWO
10: 11
*****


2. Although we'd rather use VIM and open source tools, on the subject
Windows machine where no additional software can be installed,
Microsoft Word is able to compare files and show differences at the
character level, if that is installed. Look for "Compare Documents"
in online Help.

Hopefully, you'll be able to install open source tools at some point.
If possible, Cygwin and PuttyCYG is a great combination.

========Keith

Reply all
Reply to author
Forward
0 new messages