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

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 by formatexpr (or the built-in line-breaking algorithm when formatexpr is 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 textwidth in your vimrc for the file types you write prose in: autocmd FileType gitcommit setlocal textwidth=72
  • gw is a variant that keeps the cursor in place after formatting (useful in mappings)
  • If formatexpr is set (e.g., by an LSP), gq delegates to it — use gw for the built-in formatter

Next

How do I create command-line aliases so that typos like W are automatically corrected to w?