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

How do I reformat a paragraph or motion to fit within the text width without moving my cursor?

Answer

gw

Explanation

The gw operator reformats text to fit within textwidth, just like gq — but with one key difference: gw leaves the cursor in its original position after formatting, while gq moves the cursor to the end of the reformatted area. For writers and developers who frequently reflow prose or comments, gw is often more convenient because it doesn't require repositioning the cursor afterward.

How it works

  • gw — the format operator; accepts any motion or text object
  • gw + ip (gwip) — reformat the inner paragraph under the cursor
  • gw + w (gww) — reformat the current line
  • gw + j (gwj) — reformat the current line and the one below

The reformatting respects textwidth, formatoptions, and formatprg — the same settings that govern gq.

Example

Given a paragraph with lines of varying length and textwidth=72 set:

This is a very long line that should be broken at the configured textwidth for cleaner diffs.
Short line.

With the cursor anywhere inside, gwip reformats to:

This is a very long line that should be broken at the configured
textwidth for cleaner diffs.
Short line.

The cursor returns to where it was before the command.

Tips

  • Use gw in insert mode via <C-o>gwip to reformat without leaving insert mode
  • gq is preferable when you want cursor movement as a visual confirmation of what was reformatted
  • Both operators respect formatexpr in Neovim, allowing language-aware formatting when set

Next

How do I safely use a filename containing special characters in a Vim Ex command?