How to jump to the start or the end of visual selection in Vim?
Is there a motion for moving to the start or end of a visual selection?
I know that o while in visual mode alternates between the two, but I need to be able to select precisely the start.
The overall goal is to surround a visually selected area with parentheses.
Follow-Up:
Based on the comments, I was able to implement this using the following macro. The idea is to:
- Esc to exit visual mode;
-
`>
to go to the end of the previous visual selection; -
a)
to append a closing parentheses; - Esc to exit insert mode;
-
`<
to go to the start of the previous visual selection; -
i(
to insert an opening parentheses; - Esc to exit insert mode again.
For example:
map \q <ESC>`>a)<ESC>`<i(<ESC>
Based on another comment, we have an even more concise solution:
map \q c()<ESC>P
There are two relevant built-in marks holding the positions of the first
and last characters of the last visual selection in the current buffer.
In order to move the cursor to these marks, use the commands `<
and `>
, respectively (see :help `>
and :help `<
).
While you are in Visual Selection
click o. It will change position of cursor to other end of selection. Then O to jump back.
The easiest way to "surround a visually selected area with parentheses" is:
c
hange the visually selected area to ()
and P
ut it back in the middle: c()<ESC>P
I suggest defining in the .vimrc
file a new visual-mode command (e.g., \q
) with that:
:vmap \q c()<ESC>P
This approach also works with visual rectangular areas (<C-V>
): it
puts (
and )
around each block-line.