Reformatting each line independently (how to apply an operation to each line)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Reformatting each line independently (how to apply an operation to each line)

J S-3
Every so often, I have the need to reformat a text in such a way that each line gets fed to the reformatter (e.g., gqq) by itself.  I.e., I don't want to reformat the whole text as a single block.

Is there any general way to say "Do this thing (gqq) to each line?"

I have found a workaround, which is to learn a macro (qq) to be gqq<cr>, then do:

30@q

(assuming 30 is enough)

This works, but it seems like there should be a cleaner way - that doesn't involve learning a macro.

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/631182324.5195247.1583455467154%40mail.yahoo.com.
Reply | Threaded
Open this post in threaded view
|

Re: Reformatting each line independently (how to apply an operation to each line)

Gary Johnson-4
On 2020-03-06, 'J S' via vim_use wrote:

> Every so often, I have the need to reformat a text in such a way
> that each line gets fed to the reformatter (e.g., gqq) by itself.
> I.e., I don't want to reformat the whole text as a single block.
>
> Is there any general way to say "Do this thing (gqq) to each line?"
>
> I have found a workaround, which is to learn a macro (qq) to be
> gqq<cr>, then do:
>
> 30@q
>
> (assuming 30 is enough)
>
> This works, but it seems like there should be a cleaner way - that
> doesn't involve learning a macro.

How about this?

    :g/./normal gqq

According to ":help normal-range", the following should have worked,
too, but it didn't include the last line(s) of my test file when
gqq split earlier lines .

    :%normal gqq

Those reformat the entire buffer.  You can change the range to suit.

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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200306012543.GA24028%40phoenix.
Reply | Threaded
Open this post in threaded view
|

Re: Reformatting each line independently (how to apply an operation to each line)

JohnBeckett
> :%normal gqq

Interesting. What goes wrong is that the % operates on a certain number of lines. If the buffer has, say, 5 lines that are three times the length of the current window width, then the command will operate on the first 5 lines only.


However, each gqq will replace the line with three lines and the % will stop after it has processed 5 of what results.

John

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/621daa99-7c03-42fd-9a88-27a5c87b4c8a%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Reformatting each line independently (how to apply an operation to each line)

J S-3
In reply to this post by J S-3
Thanks for all the replies. It seems that:

1) There's no really clean way to do it. It seems to me that there should be something like "linedo" - analogous to "windo" and "buffdo". Of the workarounds, the %g/./normal
method seems the best. Thanks for that.

2) It just occurred to me that, for this specific case (reformatting each line), you could use "fmt -s" (at least on Unix/GNU version of "fmt").

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/2099308128.5943971.1583595399960%40mail.yahoo.com.
Reply | Threaded
Open this post in threaded view
|

Re: Reformatting each line independently (how to apply an operation to each line)

Tim Chase
On 2020-03-07 15:36, 'J S' via vim_use wrote:
>  Thanks for all the replies. It seems that:
>
> 1) There's no really clean way to do it. It seems to me that there
> should be something like "linedo" - analogous to "windo" and
> "buffdo". Of the workarounds, the %g/./normal method seems the
> best. Thanks for that.

The typical way to write a "linedo" command is

  :g/^/command

which is subtly different from

  :g/./command

in the event that a line is empty.  For your use-case, it doesn't
change anything, but if your change should also happen on empty line,
you'd want to use the "^" pattern because every line has one of those.

> 2) It just occurred to me that, for this specific case
> (reformatting each line), you could use "fmt -s" (at least on
> Unix/GNU version of "fmt").

which, if it would work, you could pipe the range through fmt:

  :%!fmt -s

Always nice to have options :-)

-tim


--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200307095745.079f3629%40bigbox.attlocal.net.