Saturday, May 18, 2013

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

On 5/18/2013 11:13 PM, Tim Chase wrote:
> On 2013-05-18 20:22, ping wrote:
>> 1) :'<,'>g/.*/exec "normal! o\<esc>"
>> unconditionally insert a new line after each line (this may bring a
>> lot of empty lines , if the original book already contains empty
>> lines)
> Any line matching ".*" could be written as just "^", and the
> "normal..." bit can be done with an ex command, making a simpler
> version:
>
> :'<,'>g/^/put_
>
> (put the null "_" register)
>
>> 2) format all each line seperately.
>> :'<,'>g/.*/exec "normal! gqq"
>> this is different than gggqG (this will mess up everything :D )
> and again here, using "^" instead of ".*"
>
> -tim
>
>
>

hi tim:
thanks for the improvement, I tested and your commands works.

I have 2 questions here:
1) about the magic "put_"
I couldn't understand, how come "put" a null register "_" will put a new
line?
my understanding is if it's a null then it shouldn't do anything "in
theory" right...
what's the idea behind this?

:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works linewise, thus
this command can be used to put a yanked block
as new
lines.
The cursor is left on the first non-blank in
the last
new line.
The register can also be '=' followed by an
optional
expression. The expression continues until the
end of
the command. You need to escape the '|' and '"'
characters to prevent them from terminating the
command. Example: >
:put ='path' . \",/test\"
< If there is no expression after '=', Vim uses the
previous expression. You can see it with ":dis =".

2) now with the new way I have:

'<,'>g/^/put_
'<,'>g/^/exec "normal! gqq"
'<,'>s/^\s*\n\n\+/\r/

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>

but when I tested it report errors:

14 more lines
Error detected while processing function FormatBook:
line 2:
E121: Undefined variable: normal
E15: Invalid expression: normal! gqq
line 3:
E486: Pattern not found: ^s*^@^@+

seems something wrong with 2nd line:
exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"

I know this is weired , but how to generalize this to make it work with
range via a simple key map?

thanks!

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

Post a Comment