VimTrick: Reselect last visual selection
Try this quick trick to make a new edit on something you just selected.
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 us know in the comments.
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
Your comments are really helpful to us. We’ve heard a lot of calls for things that can be done without plugins, so we’re focusing on that. But there is also demand for advanced Vim tidbits, even those that require plugins and special config. What do you want to see in VimTricks? Reply to us here or comment on the post. Thanks!
To me "gf" means "Get File", so... "Get last Visual"?
I don't have any good mnemonic for this one. `g` is always a bit magical for me. It works wonderfully to do lot's of cool things but I have no idea what the g stands for or what to think of it.