Reflowing comment blocks with vim
Asked Answered
G

2

45

I'm cleaning up someone else's code and working with vim. I'd like to reflow the comments so that they're formatted consistently.

For example:

    # one two three four five six seven
    # eight nine
    # ten eleven twelve thirteen fourteen
    # fifteen sixteen seventeen 
    # eighteen
    # nineteen

Would become:

    # one two three four five six seven eight nine ten eleven 
    # twelve thirteen fourteen fifteen sixteen seventeen
    # eighteen nineteen

So, a workflow that would easily uncomment the code, reflow the paragraph and then re-comment the code with the indentation preserved. If there is an existing plugin which does exactly this, that would be great.

Guanase answered 8/12, 2011 at 18:29 Comment(0)
S
73

The gq command takes comments and indentation into account. I normally use V to visually select the lines (with k and j) and then press gq. The textwidth option is used to format the lines at the appropriate length. See :help gq for more information.

Syndicate answered 8/12, 2011 at 18:36 Comment(7)
Thanks very much for this. I had tried this before, but it didn't work for me. I just tried it in a fresh account without a .vimrc and it works perfectly. I guess I'll figure out where my defaults are getting in the way.Guanase
So, it turns out there was a ":set paste" in my .vimrc that was messing with the formatting. Mystery solved and I can happily reflow my comments. :)Guanase
Also ensure you have formatoptions set to include t, c, and q. I just spent an hour trying to figure out why this wasn't working because my formatoptions were set to l only.Sherrod
I like the sequence '{gq}' This will reflow the current paragraph ({ and } navigate to beginning and end of current para, respectively)Lopsided
Is q short for something? Or does anyone have a mnemonic for remembering it?Vannessavanni
@reedstrm: You can also use 'gqip' for the same effect. 'ip' stands for 'inner paragraph'.Blowfish
@Vannessavanni i've only ever seen the docs refer to it as the q command. suggestions: quintessential as in make the selection perfectly representative of well formatted content. qt like cutie from prettify. quotable as in put the content into a form that is quotable to or shareable with others. qualify as in to make fit for (the) special purpose (of being readable, maintainable code); or, as in to make less harsh or strict--in the sense of the degree of style quirks you subject others to, rather than syntax conformity). quillwork as in the ornamental work of text reflow.Fawn
Q
1

There is a built-in command text reflow:

gwip - reflow paragraph

So when you have this:

# one two three four five six seven
# eight nine
# ten eleven twelve thirteen fourteen
# fifteen sixteen seventeen 
# eighteen
# nineteen

It becomes this:

# one two three four five six seven eight nine ten eleven twelve thirteen
# fourteen fifteen sixteen seventeen eighteen nineteen

It will also break long lines into smaller ones.

Quebec answered 24/10, 2022 at 20:1 Comment(1)
Neat. This is pretty similar to gqip as suggested in one of the comments on the original answer, but it looks like this one keeps the cursor in the original position afterwards.Guanase

© 2022 - 2024 — McMap. All rights reserved.