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

How do I hard-wrap a paragraph to fit within textwidth without moving my cursor away from it?

Answer

gwap

Explanation

The gw operator reformats text to fit within 'textwidth' — identical in effect to gq, but with one key difference: the cursor returns to its original position instead of landing at the end of the reformatted text. Combined with ap (around paragraph), gwap reflowsthe current paragraph and immediately puts the cursor back where you started.

How it works

  • gw — format operator that preserves cursor position after formatting
  • ap — "around paragraph" text object (the full paragraph plus surrounding blank lines)
  • Together: reflow all lines in the paragraph to fit within textwidth, cursor stays put

The contrast with gqap:

  • gq moves the cursor to the last line of the reformatted block
  • gw leaves the cursor exactly where it was before the command

This matters when you are editing mid-paragraph — for example fixing a word inside a long comment — and want to reflow without having to navigate back to continue editing.

Example

With :set textwidth=72 and a run-on comment:

# This is a long explanatory comment that goes well past the column limit and really should be wrapped properly.

Press gwap to get:

# This is a long explanatory comment that goes well past the column
# limit and really should be wrapped properly.

The cursor stays on the first line at its original column.

Tips

  • textwidth must be non-zero for gw to reflow lines; :set textwidth=80 is a common setting
  • gww reformats just the current line
  • gwG reformats from the cursor to the end of the file — useful for reflowing large prose sections
  • gq remains the right choice when you want the cursor at the end of the reformatted block so you can keep writing

Next

How do I open the directory containing the current file in netrw from within Vim?