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

How do I reselect the last visual selection?

Answer

gv

Explanation

The gv command reselects the exact same area that was last selected in visual mode. This is incredibly useful when you need to perform multiple operations on the same block of text without manually reselecting it each time.

How it works

  • gv restores the last visual selection, including the mode (character, line, or block)
  • The cursor returns to the same position it was in when you left visual mode
  • The selection boundaries are remembered even after performing an operation

Example

Given the text:

first line
second line
third line

You select all three lines with ggVG and indent them with >. The selection disappears after the indent. Pressing gv reselects the same three lines so you can indent again with >, or perform a different operation entirely.

Tips

  • Use gv after indenting with > or < to reselect and indent again without pressing .
  • After yanking a visual selection with y, press gv to reselect and perform another action like d or U
  • In visual mode, press o to jump the cursor between the start and end of the selection — this works with gv too
  • Use gv followed by :sort to sort the same lines you previously selected
  • If you accidentally exit visual mode with <Esc>, gv brings the selection right back
  • gv remembers which visual mode was used — if you last used <C-v> (visual block), gv restores a block selection

Next

How do I edit multiple lines at once using multiple cursors in Vim?