How to execute Vim replace command without effect of current search pattern?

I want to go to lines containing a specific pattern and determine whether to apply the: s command.

But after using the: s command, the '/' register was replaced with a pattern in the: s command, and it was not very convenient to jump with the original pattern.

So, does he have a convenient way to accomplish this task?
Thank!

+3


source to share


3 answers


You can achieve this with a vim-script. Something along these lines should help:



function! s:CustomSearch()
    let oldsearch = @/
    :%s/pattern/replace/flags
    let @/=oldsearch
endfunction

      

+1


source


Vim (version 8+) provides a command :keeppatterns

that prevents your command from changing the search case. Example:

:keeppatterns %s/foo/bar/g

      



For details see :h :keeppatterns

.

+1


source


The Vim ( :help histdel

) manual provides another way to remove the last search pattern:

call histdel("search",-1)
let @/ = histget("search",-1)

      

0


source







All Articles