12

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?

1 Answer 1

18

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
7
  • 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, 2016 at 10:27
  • 3
    Note that you can also use gN to visually select the previous match.
    – statox
    Apr 6, 2016 at 11:52
  • What's the difference between this and just pressing n to get to the next search result? Apr 6, 2016 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, 2016 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! Sep 20, 2016 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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