I’m looking for a convenient way to fix comments where line lengths exceed a certain number of characters in Vim. I’m fine with doing this manually with code, especially since it’s not that frequent, plus refactoring long lines is often language, or even code-style dependent, but with comments this is pure drudgery.
What happens is I often spot some issue in a comment, tweak one or two words and the line spills out of the, say, 80 character limit. I move the last word to the next line and then the next line spills, and so on. Does anyone know a way to do this automatically in Vim?
I would recommend putting the following into your vimrc if this is a regular issue:
This maps the leader f shortcut (f is for “format”) to format the comment (considered a paragraph after setting some formatoption flags) with gq which formats the comment to be the width of the currently set
textwidthortwoption. You should set textwidth in your .vimrc withtextwidth=80.Formatoptions is the other thing you should fiddle with, specifically in your case by adding the
acqflags withformatoptions+=acq. Be careful to remove thetflag withformatoptions-=tbecause that will automatically wrap all of your code, not just recognized comments. After doing all this you should be able to hit f and format inside only the comment, irrespective of whether is is surrounded by blank lines.Here is the relevant info on formatoptions so you can make your own choices.