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 formattingap— "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:
gqmoves the cursor to the last line of the reformatted blockgwleaves 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
textwidthmust be non-zero forgwto reflow lines;:set textwidth=80is a common settinggwwreformats just the current linegwGreformats from the cursor to the end of the file — useful for reflowing large prose sectionsgqremains the right choice when you want the cursor at the end of the reformatted block so you can keep writing