Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

  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 
like image 380
duckworthd Avatar asked Jun 24 '12 08:06

duckworthd


People also ask

How do I jump to the next word in Vim?

You'll find that Shift-Left/Right and Ctrl-Left/Right will do the trick. Now use CTRL-H and CTRL-L to move by word in insert mode.

How do I use visual mode in Vim?

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim's Normal mode.

How do I search for a selection in Vim?

When you press * ( Shift + 8 ) in Normal mode, Vim will search for the word (i.e. keyword ) under the cursor. Assuming the text you want to search is a single word, this is a great way to search.


1 Answers

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

like image 68
ib. Avatar answered Oct 03 '22 22:10

ib.