DEV Community

Colin Bartlett
Colin Bartlett

Posted on • Originally published at vimtricks.substack.com

1 1

VimTrick: Reselect last visual selection

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:

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

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

And if you like this trick, I'm publishing a twice-weekly newsletter called VimTricks.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay