Sunday, May 19, 2013

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

On 2013-05-19, ping wrote:
> 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.

Isn't that explained in those two lines? _ is an empty register.
":put _" puts the contents of that register in a new line
("linewise") after the current line. Hence, ":put _" creates a new,
empty line.

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

I don't understand why you're going through all these gyrations to
perform what appears to be a simple task, as I answered originally.
If you want a set of lines to be formatted as paragraphs, with each
line as its own paragraph and separated by single blank lines, all
you have to do is first separate each line from the others by single
blank lines, which can be done by this:

:s/\n\+/\r\r/

with any range that you like. If some of your blank lines are not
empty (i.e., contain spaces), use this instead:

:s/\(\s*\n\)\+/\r\r/

Once all your paragraph lines are separated by blank lines, there is
no need to format each one individually; you can reformat them all
at once. Further, if you previously selected the lines visually,
you can reformat that same visual region with this normal-mode
command:

gvgq

Putting those together with a visual-mode mapping yields this:

:vnoremap ,bf :s/\(\s*\n\)\+/\r\r/<CR>gvgq

I don't care what solution you finally adopt. Vim offers many ways
to solve the same problem. I do care that you understand what
you're doing. I also care that I understand what I'm doing, so when
a solution I propose doesn't seem to work, I like to understand why.

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:

Post a Comment