Wednesday, May 12, 2010

Re: how to rearrange text file

On 11 Maig, 20:31, Tim Chase <v...@tim.thechases.com> wrote:
> Once you have that...
>
> > - Every space that's in a line must be counted, placed upfront the
> > line, and by the number + 1 needs to be done.
>
> You can use the following:
>
>    :%s/.*/\=strlen(substitute(submatch(0), '\S\+', '', 'g')).'
> '.submatch(0)
>
> to prepend the space-counts.

How would be the performance of such a formula when processing a big
file? I came upon another more or less obvious solution -again, with
some simplifications like not making distinction among spaces and
tabs. I'm using a macro:

yyP:s/\S//g<CR>"=col('$')<CR>pJ

where <CR> represent an actual Return key press when recording the
macro. Short explanation:

yyP duplicates the current line
:s/\S//g<CR> deletes all non-space characters from the (upper)
duplicated line
"=col('$')<CR>p inserts the column number the cursor is at, which
happens to be the number of spaces + 1
J concatenates the upper line (consisting of spaces and the number)
and the (original) lower line

I run this macro on all lines (99999 times or so) and at the end I
suppress the leading spaces with

:%s/\s//

Without having made any benchmarking, I *suspect* that this can be
quicker than using strlen, and submatches. I'd like to hear your
opinion about this.

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