VimTrick: Tidy Paragraphs

Format paragraphs quickly and easily with gw and gq

One of the things we’ve learned by from our readers is that there are a good number of folks that use Vim for non-programming related writing. A handy trick to keep your prose tidy is to use gw and gq to format paragraphs. Although more applicable to note taking and other writing in Vim, this trick works equally well if you’re coding. Use gw and gq to format paragraphs of text inside your README, some code comments, or text inside HTML.

  • gw - Format and wrap. Keep your cursor where it is when done.

  • gq - Format and wrap, but move your cursor to the end when done.

Both of these handy operations will format text based on a motion or visual selection. Here’s a screencast, showing the formatting of a paragraph:

Remember the mnemonic “ap” for “around paragraph” and combine that with these commands to get gqap and gwap. This will nicely format the current paragraph.

Notice that the difference between the two is where the cursor lands. If you’d prefer your cursor stay in the the same spot in the formatted text, use gw, if you’d like to have the cursor at the end of the paragraph, use gq.

Was this useful? Help us improve!

With your feedback, we can improve VimTricks. Click a link to vote: