Visual selection is a powerful tool for applying commands to specific regions of a buffer. But it can be frustrating to have selected the wrong portion and need to start your selection over. Many times I’ve had to hit <Esc>
and restart a visual selection because I realized my selection didn’t start at the right point. But this frustration can be easily remedied by making use of o
:
Pressing
o
while visually selecting moves the cursor to the opposite end of the selection.
Instead of pressing <Esc> and starting over, I can press o
to move the cursor back to the beginning of the visual selection. From there, I can use the usual suspects to adjust the beginning of the visual selection. For example j
or k
, or <C-u>
, or perhaps ?^3
. If I want to get back to the bottom of the visual selection and select more, I can press o
to bounce to the other end. This trick has remedied a frustration I’ve long had with using visual mode.
Check out the gif at the bottom of this message. In it, I incrementally select some text that I wish to move to the left. But I missed the first few lines, so I press o
to move my cursor to the back of the selection, then press k
to adjust the top boundary.
Need Reinforcement?
If you’re like me, you need tips like this dripped out you regularly in order remember them. If you follow @vim_tricks on Twitter, you’ll get at least one Vim trick from our archives every single day. Plus links to other Vim resources to help you improve.
Tomorrow, November 27, we will also offer a very brief, deeply discounted sale on Git Better with Vim, our email course that teaches you how to stay in Vim for virtually all Git operations. This discount will only be mentioned on Twitter so you’ll definitely want to follow us there.
And don’t forget that Vim for Rubyists, and Master Vim Note-Taking are both available for pre-order at 25% off with code “preoder”. We are thrilled to be able to keep these tips going with all your support so far - thank you!
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
I want to add that O in visual-block mode does something similar. It jumps to the opposite horizontal end of the visual selection, instead of the opposite vertical.
Also, gv reselects the previous visual selection which is very useful.