Vim/Vi 中是否有将光标移动到搜索突出显示结尾的命令?

Vim/Vi 中是否有任何命令可以在选定的搜索段内移动?

例如,如果我搜索一个单词,是否有任何命令将光标移动到突出显示的部分的末尾?假设我有一个单词“ FishTaco”,我想搜索“ Fish”的所有实例,并在它后面插入一些内容。我知道我可以进行全局替换,但是如果我只想在几个非顺序实例中进行更改,那该怎么办呢?

我可以看到它将是方便的,能够移动光标的结束,当前突出显示的部分,以执行一个动作。

14222 次浏览

You can do it with this:

/Fish/e

The /e at the end lands your cursor at the end of the search region (instead of the first character by default.

If you just want to place your cursor after the last character of Fish then you could use

/Fish/e+1

which will place your cursor after the h. (Without the +1 the cursor will end up to the left of the last character.)

If you are particularly interested in placing the cursor after Fish, but only when it appears in "FishTaco" then you can try one of the following options:

You can use

/FishTaco/s+4

and your cursor will end up between Fish and Taco. The /s+4 places the cursor 4 places after the start of the search term.

You could likewise use

/FishTaco/e-3

which will place your cursor 3 places left of the left side of the last (end) character.

You can also use

/FishTaco/b+4

because /b+4 will be treated as /s+4.

Once you have a visual selection, you can use o to toggle the cursor position between the start and end of the selected block. When the selection is line-wise this toggles the line position, meaning the cursor moves to the starting and ending line of the selection but not to the starting or ending character. When the selection is character-wise then the movement is also character-wise.

You can also use `< and `> to toggle the cursor between the start and end of the visual block, however this affects the selection itself, so it is used to reset or adjust the selection bounds.

Edit: Ah, but for search highlights this won't work, because o will initiate a line insertion. I guess search highlight isn't the same as a visual selection, sorry :)

The "cursor at the end of the match" part of your question is already answered.

You can do :%s/Fish/FishTaco/c. This gives you an opportunity to confirm or reject every substitution and skip the whole "cursor at the end of the match" business.

If you have already searched for /Fish, you can quickly change the search to go to the end of the match via //e, which repeats the last search, but appends the e modifier that has already been explained.

/ long-regexp1 \zs long-regexp2 / .. cursor will stop at \zs, see :help \zs