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 objectgw+ip(gwip) — reformat the inner paragraph under the cursorgw+w(gww) — reformat the current linegw+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
gwin insert mode via<C-o>gwipto reformat without leaving insert mode gqis preferable when you want cursor movement as a visual confirmation of what was reformatted- Both operators respect
formatexprin Neovim, allowing language-aware formatting when set