How do I reindent the previous visual selection and keep it selected?
Answer
gv=gv
Explanation
When you are iterating on indentation, repeating selection steps is wasted motion. gv=gv reapplies indentation to your last visual range and leaves that same range selected, so you can chain additional formatting or structural edits immediately. This is especially effective after paste operations or manual refactors where indentation needs two or three quick passes.
How it works
- First
gvrestores the previous visual selection =runs Vim's indent operator over that selected range- Final
gvreselects the same range again after indenting - Result: your block is formatted, and you stay focused on it for the next action
Example
You just pasted a code block and indentation is mostly correct but not perfect. Run:
gv=gv
The block is reindented according to your indent settings and remains selected. You can then immediately run another visual command, such as shifting (>/<) or filtering (!).
Tips
- Pair with repeat workflows: after
gv=gv, run another visual command without reselecting - If
=does nothing, check filetype and indent settings (:setlocal indentexpr? cindent?) - This pattern works best when your previous action created the range you still want to refine