Friday, December 30, 2011

Re: RegExp - Matching comments doubt

On Dec 30, 9:47 am, Fernando Basso <fernandobasso...@gmail.com> wrote:
> Suppose I have:
>
> <!-- two
>        lines --> ?
>
> This works:
>
>     g:/<!--\_.\{-}-->/ delete
>

I honestly cannot say why this would work for you. It certainly does
not "work" for me, at least not in the way you seem to expect it to.

I assume you meant :g/<!--\_.\{-}-->/ delete, which I tested, and it
only deleted the first line, as I expected.

> But why isn't
>
>     :g/<!--\_.\{-}> /delete
>
> deleting until the >
>
> Also, If I try to delete until the last > in the file:
>
>    :g/<!--\_.*>/ delete
>
> is not deleting until the last >. I don't understand why...

The reason none of these work, is because you are using the :g command
with a multi-line pattern and expecting it to operate on every line.
The :g command does not work that way. The :g command works by first
finding every line starting a match for the pattern (in this case, the
first line only) and then performing your given operation on the lines
found. It does not operate on any but the first line of a multi-line
match. There are a couple of ways you could do this.

1. Give a range to your delete command. Something like:

:g/<!--\_.\{-}-->/ .,/-->/delete

2. Use an :s command instead. Something like:

:%s/<!--\_.\{-}-->//

This being Vim, there are probably a dozen more ways as well, but
these are the two which immediately spring to mind.

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

No comments: