Monday, May 20, 2013

Re: vim: format each long lines and insert just one break afterward

On 2013-05-20, ping wrote:
> this look much simpler, quick and works great. good to learn!
>
> On 05/19/2013 02:30 PM, Gary Johnson wrote:
>
> >Putting those together with a visual-mode mapping yields this:
> >
> > :vnoremap ,bf :s/\(\s*\n\)\+/\r\r/<CR>gvgq
>
>
> still, just for a learning purpose, I'm curious about what's wrong
> with the following script?
>
> since I'm satisfied with this solution and I want to make it an
> easier bind, so I make this:
>
> function! FormatBook(...) range
> exec a:firstline . "," . a:lastline . "g/^/put_"
> exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
> exec a:firstline . "," . a:lastline . "s/^\s*\n\n\+/\r/"
> endf
>
> com! -range=% -nargs=* FormatBook :<line1>,<line2>call
> FormatBook(<f-args>)
> map ,bf :FormatBook<CR>
>
>
> specifically, I want to learn what's the right way to re-write the
> following line in a ranged func ?
>
> exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"

The action of the first exec is to expand and execute its arguments,
which in this case is a single argument, the string

a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"

Assuming for sake of a concrete example a firstline of 123 and a
lastline of 456, that string expands to this:

123,456g/^/exec normal! gqq

Note that this second exec sees "normal!" and "gqq" not as strings
but as variables. That is what is causing the error message you
reported previously.

You don't need that second "exec" at all, so a better way to write
that command is this:

a:firstline . "," . a:lastline . "g/^/normal! gqq"

Regards,
Gary

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: