Search code examples
vimnotepad++

find and replace VIM but with interactively previous and next word like notepad++


In notepad++, you can simply ctrl+h to find and replace, where first field asking what word, and second field asking what should it replace. Also, there's arrow up (previous) and arrow down (next).

That arrow button is useful because i can decide what line should i replace and what line shouldn't i replace, if i click arrow down button for example, it will select a word from previous line to next line. And if i click replace it will replace that line.

How do i do it in VIM?

I only find that arrow feature in search VIM feature with prefix /wordFind with click n for next and shift+n for previous, but i dont find such feature in :s/wordFind/replacerWord command


Solution

  • The simplest Vim equivalent would be the /c flag:

    :%s/foo/bar/c
    

    which allows you to accept or reject each substitution:

    sr

    See :help :s_flags. It is limited to "Next", though.

    Note that, if you use Gvim, you can get a modal window, like in Notepad++, with a "Next" button but no "Previous" button:

    sr2

    FWIW, the MacVim GUI's equivalent of that modal window actually has a "Previous" button, for some reason:

    sr3

    Note that those windows are more limited in functionality than the one in Notepad++. I guess they exist more as a courtesy to casual users than anything. The manual way is the real deal, in Vim and, from experience, it is much smoother and powerful.