如何在 Vim 中跳转到视觉选择的开始或结束?

是否有移动到视觉选择的开始或结束的动作?

我知道在视觉模式下 o会在两者之间交替,但是我需要能够精确地选择开始。

总体目标是用括号围绕视觉选定的区域。


跟进:

根据注释,我可以使用下面的宏来实现它:

  1. Esc退出视觉模式;
  2. `>转到前面视觉选择的末尾;
  3. a)附加一个括号;
  4. Esc退出插入模式;
  5. 走到 `<的开始,以前的视觉选择;
  6. i(插入开口括号;
  7. Esc再次退出插入模式。

例如:

map \q <ESC>`>a)<ESC>`<i(<ESC>

根据另一条评论,我们有一个更简洁的解决方案:

map \q c()<ESC>P
17760 次浏览

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

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.

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)<Esc> to insert a closing parenthesis after the last character of the selection.
  4. Type `<i(<Esc> to insert an open parenthesis before the first character of the selection.

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:

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.