Tidy Paragraphs


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 gqto 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.

How useful was this tip?

Average rating 3 / 5. Vote count: 547

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

36 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts