Is there a command in Vim / Vi to move the cursor to the end of the search highlight?

Does Vim / Vi have commands to move to the selected search segment?

For example, if I search for a word, are there any commands to move the cursor to the end of the selected segment? Say I have the word "FishTaco" and I want to search for all instances of "Fish" and insert something after it. I know I can make a global replacement, but what if I only want to make a change to a couple of unclassified instances?

I could see where it would be convenient to move the cursor to the end of the current selected segment to perform the action.

+69
vim vi
May 22 '12 at 18:10
source share
6 answers

You can do this with this:

/Fish/e

/e at the end places your cursor at the end of the search area (instead of the first character by default.

+104
May 22 '12 at 18:22
source share

If you just want to place the cursor after the last Fish character, you can use

 /Fish/e+1 

which will place your cursor after h. (Without +1 cursor ends 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", you can try one of the following options:

you can use

 /FishTaco/s+4 

and your cursor will be between Fish and Taco. /s+4 places the cursor 4 places after the start of the search query.

You can also use

 /FishTaco/e-3 

which will place the cursor 3 places to the left of the left side of the last (final) character.

You can also use

 /FishTaco/b+4 

because /b+4 will be considered /s+4 .

+67
May 22 '12 at 18:25
source share

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 adds the modifier e , which has already been explained.

+24
May 23 '12 at 5:54 a.m.
source share

Once you have a visual choice, you can use o to switch the cursor position between the beginning and end of the selected block. When selecting in turn, this switches the position of the line, that is, the cursor moves to the start and end lines of the selection, but not to the start or end character. When the choice is characteristic, then the movement is also characteristic.

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

Edit: Ah, but for the search it is emphasized that this will not work because o will start setting the line. I think that search is not the same as visual selection, sorry :)

+11
May 22 '12 at 18:33
source share

The answer to the "cursor at the end of the match" is already set.

You can do :%s/Fish/FishTaco/c . This gives you the opportunity to confirm or reject each substitution and skip the entire β€œcursor at the end of the match” business.

+7
May 22 '12 at 10:01
source share

/ long-regexp1 \zs long-regexp2 / .. the cursor stops at \ zs, see help \ zs

+7
Jun 27 '15 at 14:21
source share



All Articles