12

I really enjoy Vim's incsearch functionality. Is there a way to have it, or something similar to it, working while I do a replacement?

For example, if in normal mode I type:

:%s/potato/

Can I get all the potatos highlighted on my file, just like they are highlighted by incsearch when I write

/potato

?

This would already help me a lot when using regular expressions. A very very nice-to-have would be that when I write this:

:%s/potato/banana/

It would replace the potatos with bananas, and highlight them - maybe the replacements should be in a different color. It sould not "commit" the change until I pressed enter though).

I'm using neovim at the moment, in case it helps.

  • 1
    Do you mean incsearch? hlsearch should work ootb, if I type :%s/potato/\0/ to replace potato with itself, then I see all matches highlighted. – jjaderberg Feb 2 '17 at 0:33
  • Inded, I meant incsearch. Replacing in my question, thanks. – kikito Feb 2 '17 at 0:49
  • It's probably not kosher but :/pattern<CR> works fine then add the %s and the replace string. Thene again so does refine the search and use CTR-R/ to paste to the search part of :%s/ – Steve Feb 2 '17 at 5:02
  • Appending to @jjaderberg: :%s/potato/carrot/c will let you jump ("confirm") through your replacements (add g to replace all instances on the same line) and with hlsearch you'll see them in advance. – Rolf Feb 3 '17 at 8:32
9

After a bit of searching, I found this NeoVim issue, which in turn was closed by this PR, which brought me to the inccommand setting. Adding this to my config file:

set inccommand=nosplit

makes NeoVim behave almost exactly as I requested in my question. I did not find something equivalent for regular Vim, but I didn't look for it very energetically.

  • 4
    In your situtaion, for incsearch in subtitutions (being a lazy Vimmer and slow to adopt neo things) I would probably search for potato (/potato) enjoying ordinary incremental search highlighighting, and then substitute with an empty search: %s//banana. But I don't know how it compares to set inccommand=nosplit in other respects. – jjaderberg Feb 2 '17 at 1:48
  • Thanks! I didn't know the "empty search trick". I will try to remember it when I'm vim on my servers – kikito Feb 2 '17 at 11:39
  • 1
    @jjaderberg by the way I liked the inccommand setting so much that I made an asciinema showing it at work, in case it motivates you to give neovim a try: asciinema.org/a/3lzlyzn23c52wwp9mk644ft20 – kikito Feb 2 '17 at 11:41
  • 2
    @jjaderberg I think that's worthy of being an answer. – Rich Feb 3 '17 at 9:43
  • Unfortunately, the link to the asciinema is dead now. – sjy Oct 26 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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