I have hlsearch set, but it sometimes lulls me into thinking t hat I have the search term visually selected, and can just hit c to change it.

The term searched for may not be an easy number of letters, words, even lines - and anyway, I don't want to count.

Is there a way to vh (or ch), say, where h runs until the current highlighting stops?

up vote 13 down vote accepted

You can use the gn motion for that, it selects the next searched element.

You can use it like so:

/foo<CR>
gn            -> select the next "foo"
sbar<Esc>     -> (optional) substitute it with "bar"

Bonus: To have . repeat the search and the change, use a c to do everything in one command:

/foo<CR>
cgnbar<Esc>
...           -> find and change the 3 next foo
  • 1
    Fantastic! Thanks. (Anyone that like me misses why . will repeat the entire action, and can't make it work - read more carefully - c first! Couldn't ask for better.) – OJFord Apr 6 '16 at 10:27
  • 3
    Note that you can also use gN to visually select the previous match. – statox Apr 6 '16 at 11:52
  • What's the difference between this and just pressing n to get to the next search result? – user1717828 Apr 6 '16 at 14:42
  • it does the change also, so instead of repeating n. (next occurence, repeat the modification), you can just press . (repeat the modification, which is "change the next occurence") – nobe4 Apr 6 '16 at 16:25
  • Indeed, when . invokes gn, the original foo has disappeared, so Vim searches for the next foo. So gn does what n would have done anyway (and more). But, as with n, note that this only works well when bar does not contain foo! – joeytwiddle Sep 20 '16 at 8:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.