vimtricks.wiki Concise Vim tricks, one at a time.

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:

  • \vvery magic: all ASCII punctuation is special (most like PCRE)
  • \mmagic: default mode, some characters are special (., *, [, ^, $)
  • \Mnomagic: only ^ and $ are special
  • \Vvery 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/g replaces the literal string foo.bar, not fooXbar
  • \V applies only to the search pattern — the replacement string in :s is never a regex, so no modifier is needed there

Next

How do I prepend a value to the front of a comma-separated Vim option like path or runtimepath?