Reply to message «Re: Delete lines matching hits», sent 19:50:20 06 February 2011, Sunday by Eduard:
> How should I use your function? I've copied it to my .vimrc but don't know
> what to do next :-)-
I thought that the only argument to this function is self-explanatory. Just do
:call DeleteMatchingLines('abc\ndef')<CR>
to delete all lines matching pattern `abc\ndef' (they are not put in any
register).
Original message:
> Hy ZyX!
>
> On Sunday, February 6, 2011 2:00:54 AM UTC+1, ZyX wrote:
>
> It should be
>
> > :%s/.*{pattern}.*\n//
> >
> > or, in this particular case:
> > :%s/.*abc\ndef.*\n//
> >
> > because we are going to delete the whole lines that contain match, but
> > nobody
> > said that they contain only match. Both solutions does not work for
> > patterns
> >
> > with \zs and \ze, maybe it is better to do the following:
> > function! s:MarkToDel(todelete, sline, submatch)
> >
> > let lnum=len(substitute(a:submatch, '\n\@!.', '', 'g'))
> > call add(a:todelete, [a:sline, lnum])
> > return a:submatch
> >
> > endfunction
> > function! DeleteMatchingLines(pattern)
> >
> > let todelete=[]
> > let savedgdefault=&gdefault
> > set nogdefault
> > try
> >
> > execute '%s/'.escape(a:pattern, '/').
> >
> > \'/\=s:MarkToDel(todelete, line("."),
> > submatch(0))'
> >
> > finally
> >
> > let &gdefault=savedgdefault
> >
> > endtry
> > let deleted=0
> > for [sline, lnum] in todelete
> >
> > let sline-=deleted
> > let deleted+=lnum+1
> > let range=sline.','.(sline+lnum)
> > execute range.'delete _'
> >
> > endfor
> >
> > endfunction
>
> How should I use your function? I've copied it to my .vimrc but don't know
> what to do next :-)
>
> Thank you again for your time (and Tim's too!)
signature.asc
Description: This is a digitally signed message part.
