Wednesday, July 18, 2012

Re: How to remove empty lines except of one

On Wed, Jul 18, 2012 at 03:46:45PM EDT, Ben Fritz wrote:
> On Tuesday, July 17, 2012 7:27:42 PM UTC-5, Bee wrote:
> > This will collapse multiple blank lines,
> > remove trailing white space, and
> > leave only one blank line at the end of file.
> >
> > %s-\_s\+$-\r
> >
> > Bill

> I found the problem with this one. :help /$ indicates that the $ is
> zero-width, so the end of line is not included in the match, therefore
> on the two blank lines at the beginning of the file, only the first is
> included in the match.

> So, this works exactly as I think it should:
>
> %s-\_s\+\n-\r

The problem is that this regex only works for blocks of empty lines at
the beginning and end of the buffer: it replaces blocks of empty lines
separated by non-empty lines (the more general case) including the eol
that terminates the last non-empty line by a single '\r'. The result is
that these blocks are not replaced by a single empty line but rather..
as seen by the user.. by nothing. IOW, all empty lines except the ones
at the beginning and end of buffer are not collapsed.. they disappear.

One could imagine that this could be addressed by adding and extra '\r':

| %s-\_s\+\n-\r\r

But then (obviously) this leaves two empty lines both at the beginning
and the end of the buffer.

> Note this is almost identical to my first proposed solution, except
> that it is missing the beginning of line anchor. I'm not sure what
> makes it fail with the ^ in place.

I feel that mixing (zero length) anchors such as '^' and '$' with actual
one character representations such as '\n' or '\_.' tends to confuse the
issue. With more typical regex processing where the input is a string
rather than an editor such as Vim's buffer, the only anchors are the
start of string and end of string..

What Vim does with '^' or '$' especially when they are part of an atom
that gets repeated via \+ .. \{2,\} .. etc. is anybody's guess..

But again, I'm NOT particularly interested in finding a regex that
actually does the job (despite my remark regarding anchors, I like Bee's
effort because it's the most compact of them all and does the part of
the job that matters in real life) .. I don't remember in 5-6 years
using Vim I have ever needed to collapse multiple empty lines..

I am more curious as to why the various regexes that I tried to put
together actually do not work, this one for instance:

| %s-\(\%^\|.*\S\+.*\n\)\zs\(^\s*\n\)\{2,\}-\r

Or rather why they appear to work¹ when I use them in a search command,
but fail on the last line of the buffer when I use them in a substitute
command.

Thanks,

CJ

¹ I set incsearch and hlsearch to visualize what actually matches and
move the cursor around the buffer.. I guess the highlighting could be
buggy in corner case circumstances.. perhaps I should look into
writing a function that grabs the actual match as seen by Vim
internally and echoes it back to the terminal.

--
AHH! The neurotic monkeys are after me!

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