Reselect last visual selection

3.2
(1271)

Often times I’m working with a visual selection of text and need to operate on that selection again. Most often this is because I’m working on a substitution pattern and don’t quite have it right. I’m swapping out some text and the regex is a bit off and I need to try again. Other times I might want to perform a substitution and then perhaps format the section with =.

Fortunately, Vim provides gv to reselect the last visual selection.

Consider the following file. It is not valid JSON. We’d like to format it as valid JSON, and improve the readability by indenting better.

The animation shows the following steps:

  1. Visual selection: shift-v %
  2. Substitution: s/\(.*\):/"\1":/g
  3. Reselect last visual selection: gv
  4. Format selection: =

Does anyone have a good mnemonic for gv? Something to help me internalize this? Maybe “a G ain V isual” but that’s a bit of a stretch. Let us know in the comments.

How useful was this post?

Average rating 3.2 / 5. Vote count: 1271

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

49 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

Leave a reply

Your email address will not be published. Required fields are marked *