How do I hard-wrap the current paragraph to the textwidth in Vim?
Answer
gq}
Explanation
The gq operator formats (hard-wraps) text to fit within textwidth columns by inserting real newlines. Combining it with } (the "end of paragraph" motion) formats from the cursor to the end of the current paragraph — exactly what you need when writing prose, documentation, or commit messages.
How it works
gq— the format operator; applies the formatter defined byformatexpr(or the built-in line-breaking algorithm whenformatexpris unset)}— motion that moves to the next blank line (paragraph boundary)- Together: format text from the cursor position to the end of the paragraph
Common gq pairings:
| Command | What it formats |
|---|---|
gqq |
Current line only |
gq} |
Cursor to end of paragraph |
gqip |
Inner paragraph (most useful, cursor can be anywhere inside) |
gggqG |
Entire file |
gq'<,'> (or gq in Visual) |
Selected text |
Example
With set textwidth=72 and a paragraph that has run-on lines:
This is a very long line that goes way past the 72-character limit and really should be wrapped onto multiple lines for readability.
Press gqip anywhere inside it:
This is a very long line that goes way past the 72-character limit and
really should be wrapped onto multiple lines for readability.
Tips
- Set
textwidthin your vimrc for the file types you write prose in:autocmd FileType gitcommit setlocal textwidth=72 gwis a variant that keeps the cursor in place after formatting (useful in mappings)- If
formatexpris set (e.g., by an LSP),gqdelegates to it — usegwfor the built-in formatter