How do I search for a string containing special regex characters like dots or asterisks without escaping them?
Answer
\V
Explanation
Vim's default search mode gives special meaning to characters like ., *, [, and ^. Searching for a literal URL, variable name, or code snippet that contains these characters normally requires careful escaping. Prefixing your search with \V ("very nomagic") disables all magic, so every character is treated literally except \ itself.
How it works
Vim has four regex modes controlled by a prefix in the search pattern:
\v— very magic: all ASCII punctuation is special (most like PCRE)\m— magic: default mode, some characters are special (.,*,[,^,$)\M— nomagic: only^and$are special\V— very nomagic: nothing is special except\and\n
With \V, you can type your pattern as-is with no escaping.
Example
Searching for the literal string file.txt?v=2 in default magic mode:
/file\.txt?v=2
With \V, no escaping is needed:
/\Vfile.txt?v=2
The search correctly finds only the exact string, without . matching any character or ? triggering the preceding character as optional.
Tips
- Combine with
<C-r><C-w>to paste the word under the cursor into the search literally:/\V<C-r><C-w> - Useful in mappings for reliable exact-word matching:
:nnoremap <leader>/ /\V - Inside a substitute, it works the same way:
:%s/\Vfoo.bar/baz/greplaces the literal stringfoo.bar, notfooXbar \Vapplies only to the search pattern — the replacement string in:sis never a regex, so no modifier is needed there