I have in my .vrimrc cute little macros which add/remove c++ style comments from code:
" remove c++ style comment
nmap _ :s/^[ \t]*\/\///<CR>==:nohls<cr>
" comment line, c++ style
nmap - :s/^[ \t]*/\/\/ /<CR>==:nohls<cr>
These work by replacing the beginning of line pattern with another. In one case adding // and in another removing the slashes (if found).
The problem I bump into is that those macros use search-and-replace. As a result, unwanted search patterns are saved into vim's search history, cluttering it.
Consider the sequence:
How can this macro be modified to not inject unwanted patterns into the search history?
Recent Vims have the :keeppattern
modifier, which will prevent the pattern from adding to the history.
let old = @/
,call histdel('/', -1)
,let @/ = old
.Like this:
" remove c++ style comment
nnoremap <silent> _ :let old = @/<bar>s/^[ \t]*\/\///<CR>==:nohls<bar>call histdel('/', -1)<bar>let @/ = old<cr>
" comment line, c++ style
nnoremap <silent> - :let old = @/<bar>s/^[ \t]*/\/\/ /<CR>==:nohls<bar>call histdel('/', -1)<bar>let @/ = old<cr>
Or use Tim Pope's Commentary.
Also, there's a search()
function, which doesn't touch the search history.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With