44

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.

oalders
  • 5,239
  • 2
  • 23
  • 34

2 Answers2

70

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.

Walter
  • 7,809
  • 1
  • 30
  • 30
  • 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. – oalders Dec 08 '11 at 18:47
  • 3
    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. :) – oalders Dec 08 '11 at 19:09
  • 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. –  Jan 15 '12 at 19:35
  • 3
    I like the sequence '{gq}' This will reflow the current paragraph ({ and } navigate to beginning and end of current para, respectively) – reedstrm Oct 16 '13 at 15:50
  • Is `q` short for something? Or does anyone have a mnemonic for remembering it? – Xiao Dec 11 '15 at 23:57
  • 5
    @reedstrm: You can also use 'gqip' for the same effect. 'ip' stands for 'inner paragraph'. – Ingo Blechschmidt Oct 30 '17 at 22:56
  • 2
    @Xiao 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. – pkfm Sep 07 '20 at 22:15
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.

supercobra
  • 15,810
  • 9
  • 45
  • 51
  • 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. – oalders Oct 24 '22 at 20:08