Monday, July 6, 2009

vim paragraph reformat (justify or align)


I like editing my plaintext files (and some of my source code) at

:set textwidth=78

The problem is that vim does not automatically justify or realign when adding/deleting text. The simple way to do it is to select the lines to format and use:

gq


Done!
Note that since gq is an operator that you can use other ways to select the text to format, but visual mode is the most intuitive for me.

You could for example do this:

gq21j

The downside is that you'd have to know the line count. A more useful text movement command to use with gq is ) (which moves you to the end of the current paragraph. So you'd do this:

gq)

2 comments:

Robert Massaioli said...

Nice post but you might have been better off with a gq}.

rr said...

Indeed. :) Thanks for the comment.