Skip to main content

Reformat paragraphs in vim

Mind blown here: For years I've been using par(1) to reformat paragraphs in vim. Which does an aesthetically pleasant job, but the documentation and configuration is mildly speaking nuts. And today for no obvious reason I came across the vim-builtin "gq" command. Which does it all and with some extra magic.

In a handful of tests, there were some cases where "par" gave a more pleasant reflow. Most of the time there is no different result. But then "gq" has some easy to understand options that are actually readably documented and... it does crazy useful stuff like being able to recognise lists (numbered, bullets) and reformat them successfully.

Want to reformat mail messages from clients that do not hard wrap lines? Why ofcows no problem! "gq" handles all the weird stuff, like not wrapping long URLs, and mixing various quote levels. It won't even mix in the attribution line with the quoted mail text.

How come I didn't discover this long ago? Yesterday I had noticed that par put in an extra space after punctuation in rare and weird circumstances. There probably is some way to configure it not to do that. But go have a look at that man page... it's out of this world! No way. Which led me to search and discover the vim-builtin "gq" command.

I use set nojoinspaces and set formatoptions+=n. Then I also have a map _r gqip to save some typing. For documentation, start with :help gq and :help fo-table.