vimdiff: How to extract deleted lines

214 views
Skip to first unread message

anokun7

unread,
May 11, 2009, 12:20:09 PM5/11/09
to vim_use
I tried using google, but couldnt get an answer, so here I am, please
suggest / help

I have 2 files (FILE 1 is an older version of FILE 2) - each contain a
list of account id's (hence are unique and sorted - let's assume) like
the following:

FILE 1
***************
ab123
bc123
bv345
jd811
jk11
ak15

FILE 2
***************
ab123
bc123
jd811
hij12
jk110
ak15

When I look at the files using vimdiff, I can see the differences
between the two files. What I want to achieve are two things - the
second is more important I think:

1. Enable line level differences: that is the line jk11 in FILE 1
should show as deleted in FILE 2.
2. Extract all deleted lines from FILE 1 into a buffer or another
file. That is: lines bv345, jd811, jk11.

Is this possible using vimdiff - I assumed that this a variant of
generating a patch, but couldnt proceed beyond that.

If (2) is possible without (1) that will also be acceptable.

Thanks a lot for any help.

Tim Chase

unread,
May 11, 2009, 12:39:01 PM5/11/09
to vim...@googlegroups.com
> 2. Extract all deleted lines from FILE 1 into a buffer or another
> file. That is: lines bv345, jd811, jk11.
>
> Is this possible using vimdiff - I assumed that this a variant of
> generating a patch, but couldnt proceed beyond that.

If they're flagged as different, I occasionally use this stunt:

:let @a=''|g/^/if diff_hlID(line('.'), 1) | sil! y A | endif

This yanks all the changed/added into the "a" register (which
means you'd have to run it in FILE2 instead of FILE1 to get the
ones that were added, because you can't yank content where it
doesn't exist :)

You're still left with the problem of getting the diff to be
linewise.

Alternatively, if the data isn't too crazy (no regexp metachars
in it) I've used

:%s@.*@:g/^&$/sil! >

to turn one buffer into a bunch of Ex commands (in this case,
shifting lines that match; but you could use "d" instead of ">"
if you want to nuke the matching lines), and then yank the entire
buffer:

:%y

then executing

@"

in the other window to replay that buffer as a macro. It's a
little down-and-dirty but it gets the job done when I need to
compare two files.

Hope this gives you some ideas...

-tim

Gary Johnson

unread,
May 11, 2009, 1:29:40 PM5/11/09
to vim_use

Vim does its best to show differences at the character level and I
don't know of a way around that.

> 2. Extract all deleted lines from FILE 1 into a buffer or another
> file. That is: lines bv345, jd811, jk11.
>
> Is this possible using vimdiff - I assumed that this a variant of
> generating a patch, but couldnt proceed beyond that.

When I need to do things like this, I use command-line tools such as
comm and diff. If the files are sorted (which your examples
aren't), you can use comm like this,

comm -23 file1 file2 > uniq1

where uniq1 will contain the lines appearing only in file1.
Alternatively, you could use diff like this,

diff file1 file2 | sed -n '/^</s/^..//p' > uniq1

Regards,
Gary


David Fishburn

unread,
May 11, 2009, 3:21:25 PM5/11/09
to vim...@googlegroups.com
On Mon, May 11, 2009 at 12:39 PM, Tim Chase <v...@tim.thechases.com> wrote:
>
>> 2. Extract all deleted lines from FILE 1 into a buffer or another
>> file.

Another option if I understand the question correctly is the
WhatsMissing plugin.
http://www.vim.org/scripts/script.php?script_id=1108

It can check at the Word or Line level.
It can also ignore case and white space.

Just load the 2 buffers and type :WhatsMissing, it will prompt you for
the options.

It is good to have options.

HTH,
Dave

anoop

unread,
May 12, 2009, 8:38:33 AM5/12/09
to vim_use


On May 11, 1:29 pm, Gary Johnson <garyj...@spocom.com> wrote:
Thank you so much, of the options I tried the comm command / option
works the smoothest for me. I could get the job done.
Reply all
Reply to author
Forward
0 new messages