How to jump to the start or the end of visual selection in Vim?
Solution 1
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 2
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 3
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 4
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
If you can't use Surrond.vim, here is one way to do it:
- Do your visual selection with
vorV. - Get out of it with
<Esc>. - Type
`>a)<Esc>to insert a closing parenthesis after the last character of the selection. - Type
`<i(<Esc>to insert an open parenthesis before the first character of the selection.
duckworthd
Updated on June 16, 2022Comments
-
duckworthd over 1 yearIs 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 -
Grant McLean over 10 yearsThis is exactly the answer I was looking for. @duckworthd - you should consider clicking accept :-) -
Adrian almost 10 yearsBut how do you keep the selection the same? -
ib. almost 10 years@Adrian: You mean without leaving Visual mode? If so, you can use theocommand. -
rsoren over 8 yearsI'm still missing the functionality I'm looking for. I'd like to move the cursor to the end of the selection without changing it.
odoes this if the cursor is at the top, but I want it to stay if it's already at the bottom.'>would work but it changes the selection -
duckworthd about 8 yearsClose, but not quite. If the last character in the visual selection is the last character on the line, the cursor hovers over the character preceding the visual selection. In consequence, using insert ("i") results in parenthesized text being inserted one character further left than intended. -
dosentmatter over 3 yearsI think it's justoto jump vertically back and forth.Ois to jump horizontally back and forth if you are in visual-block selection. -
horta over 2 years@dosentmatter Just to clarify, it looks like in visual-block selection mode, o jumps to the furthest corner from your cursor. O jumps to the farthest point on the same line (just as you describe). This allows easy access to all 4 corners of the block. -
dosentmatter over 2 years@horta, yes you are right.ois more correctly described as diagonal movement than either vertical or horizontal movement.