Restore previous Vim search term?

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?

wes

Posted 2014-09-25T20:04:49.127

Reputation: 645

Answers

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.

Brett Y

Posted 2014-09-25T20:04:49.127

Reputation: 306

I had to remove the <CR> from the function to fix an error about trailing characters, but other than that this is perfect. – wes – 2014-09-25T21:55:43.410

Opps, I'll update my answer. – Brett Y – 2014-09-25T22:13:32.993