How to jump to the start or the end of visual selection in Vim?

Vim

Vim Problem Overview


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:

  1. Esc to exit visual mode;
  2. `> to go to the end of the previous visual selection;
  3. a) to append a closing parentheses;
  4. Esc to exit insert mode;
  5. `< to go to the start of the previous visual selection;
  6. i( to insert an opening parentheses;
  7. 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

Vim Solutions


Solution 1 - Vim

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 `<).

Solution 2 - Vim

While you are in Visual Selection click o. It will change position of cursor to other end of selection. Then O to jump back.

Solution 3 - Vim

The easiest way to "surround a visually selected area with parentheses" is:

change the visually selected area to () and Put 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.

Solution 4 - Vim

if you just want to surround a visual selection there has already work been done, namely by tim pope, who wrote this plugin called surround. It surrounds words or visual selection with delimiters of your liking.

select your visual selection, say i like vim hit S) to get (i like vim) or S( to get ( i like vim ), to change this to [i like vim] type cs] (change surrounding) and to delete ds] to get i like vim at last.

Solution 5 - Vim

If you can't use Surrond.vim, here is one way to do it:

  1. Do your visual selection with v or V.
  2. Get out of it with <Esc>.
  3. Type ``>a)` to insert a closing parenthesis after the last character of the selection.
  4. Type ``` to insert an open parenthesis before the first character of the selection.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionduckworthdView Question on Stackoverflow
Solution 1 - Vimib.View Answer on Stackoverflow
Solution 2 - VimBulgantamirView Answer on Stackoverflow
Solution 3 - VimJJoaoView Answer on Stackoverflow
Solution 4 - VimepsilonhalbeView Answer on Stackoverflow
Solution 5 - VimromainlView Answer on Stackoverflow