Going to the End of a Search Highlight

I saw this on Twitter:

From: @jyurek

Sent: 27 Aug 2009 10:35

Is there a vim command to go to the end of the current search

highlight? Like e for words, but for search matches.

#vim #lazytweet

I found it quite interesting and didn’t have an “out of the box” Vim answer to this. So I crafted the following answer:

:nmap <silent> <c-e> /<c-r>//e<cr>:let @/='<c-r>/'<cr>

And that seems to work. Essentially you hit CTRL-e and that starts another search exactly like your previous search but puts the cursor at the end of it, then puts the original search back in the search register so that ‘n’ and ‘N’ work as expected.

2 comments on this post.
  1. Nikos Aggelidis:

    please delete the first post
    hi Derek!

    To set the cursor to the end of a match you can do

    /search_term/e

    also the following work:

    /search_term/e : cursor set to End of match
    /search_term/e+1 : cursor set to End of match plus 1
    /search_term/s-2 : cursor set to Start of match minus 2

  2. Derek Wyatt:

    But that’s exactly what I’ve done above. The “//e” is changing the search to go to the end. However, what I didn’t like is that it fundamentally changed the search permanently so that when I hit ‘n’ or ‘N’ it went to the end of the search, which is not what I had *originally* intended, so I put the original search back in the search register so that ‘n’ and ‘N’ did what I wanted.

    So now you can search forward with /mysearch and then hit CTRL-e to go to the end of it, hit ‘n’ to move to the beginning of the next match OR hit CTRL-e again to go to the end of the next match instead.

    Just manually typing /search_term/e is a real pain in the butt.

Leave a comment