0

Is it possible to save/restore Vim search terms in a map?

For instance, I have the following in my .vimrc:

map ,/ :s/^/\/\//<CR>

If I have turned on highlighting (:set hls) previously in the session, then when I invoke the above mapping the first column of every line will be highlighted.

Would it be possible to instead save the current search term (if any), then perform the search/replace, then restore the last search term (if any) instead?

1 Answer 1

1

If you put the substitute command into a function you won't override your search highlighting. See :h function-search-undo

map <leader>/ :call SomeName()<cr>
function! SomeName()
  s/^/\/\//
endfunction

P.S.

I assume , is your leader key.

2
  • I had to remove the <CR> from the function to fix an error about trailing characters, but other than that this is perfect.
    – wes
    Sep 25, 2014 at 21:55
  • Opps, I'll update my answer.
    – Brett Y
    Sep 25, 2014 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .