Grep without storing search to the "/ register in Vim

Posted by Phro on Stack Overflow See other posts from Stack Overflow or by Phro
Published on 2014-08-20T23:01:03Z Indexed on 2014/08/21 16:20 UTC
Read the original article Hit count: 160

Filed under:
|
|

In my .vimrc I have a mapping that makes a line of text 'title capitalized':

noremap <Leader>at :s/\v<(.)(\w{2,})/\u\1\L\2/g<CR>

However, whenever I run this function, it highlights every word that is at least three characters long in my entire document. Of course I could get this behaviour to stop simply by appending :nohlsearch<CR> to the end of the mapping, but this is more of an awkward hack that still avoids a bigger problem: The last search has been replaced by \v<(.)(\w{2,}).

Is there any way to use the search commands in Vim without storing the last search in the "/ register; a 'silent' search of sorts? That way, after running this title-making command, I can still use my previous search to navigate the document using n, N, etc.

Edit

Using @brettanomyces' answer, I found that simply setting the mapping:

noremap <Leader>at :call setline(line('.'),substitute(getline('.'), '\v<(.)(\w{2,})', '\u\1\L\2', 'g'))<CR>

will successfully perform the substitution without storing the searched text into the / register.

© Stack Overflow or respective owner

Related posts about vim

Related posts about grep