vimtricks.wiki Concise Vim tricks, one at a time.

How do I reflow or hard-wrap a paragraph to a set text width in Vim?

Answer

vipgq

Explanation

The vipgq sequence reflowing a paragraph to fit within the width defined by textwidth (default 0, meaning no limit). This is invaluable when editing prose, docstrings, or comments where you want consistent line lengths without manually breaking lines.

How it works

  • v — enters visual mode
  • ip — selects the inner paragraph (contiguous block of non-blank lines)
  • gq — applies the formatprg/formatexpr formatter to the selected region; with neither set, Vim's built-in word-wrapper joins and re-wraps lines to fit textwidth

Set your desired width before using:

:set textwidth=80

Example

Given text with arbitrary line breaks (cursor anywhere in the paragraph):

This is a very long paragraph that has
been wrapped at inconsistent points and
needs to be reflowed to a consistent width of eighty characters.

After :set textwidth=80 and vipgq:

This is a very long paragraph that has been wrapped at inconsistent points and
needs to be reflowed to a consistent width of eighty characters.

Tips

  • gqip achieves the same result without entering visual mode — the operator form works directly with text objects
  • gqq reflowing only the current line
  • gq} to reflow from the cursor to the end of the paragraph
  • :set formatoptions+=a enables automatic re-wrapping as you type (useful for prose but disruptive for code)
  • With formatprg=par or formatprg=fmt, gq delegates to an external program for smarter reflowing

Next

How do I define custom fold marker strings instead of the default {{{ }}} in Vim?