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 modeip— selects the inner paragraph (contiguous block of non-blank lines)gq— applies theformatprg/formatexprformatter to the selected region; with neither set, Vim's built-in word-wrapper joins and re-wraps lines to fittextwidth
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
gqipachieves the same result without entering visual mode — the operator form works directly with text objectsgqqreflowing only the current linegq}to reflow from the cursor to the end of the paragraph:set formatoptions+=aenables automatic re-wrapping as you type (useful for prose but disruptive for code)- With
formatprg=parorformatprg=fmt,gqdelegates to an external program for smarter reflowing