CrazySynthax
CrazySynthax

Reputation: 14988

vim: How to search for a hard-coded string (not regex)?

In order to search for a string in Vim, I click "/" and then type the word that I have to search. Vim looks at this string as regular expression. I want to know how to search a string, as it it, and not treat it as a regex.

Upvotes: 0

Views: 1032

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172510

With the very nomagic mode of Vim's regular expressions (:help /\V), only the backslash is a special character that needs escaping.

So, prepend \V to your literal search, and (either manually or via escape(pattern, '\')) duplicate any backslashes. The following turns a "regular" search in to a literal one; you could define a mapping for that:

:let @/ = '\V' . escape(@/, '\')

Upvotes: 1

phd
phd

Reputation: 94407

Search commands always search for patterns (also known as regular expressions). You can make patterns more or less magic but cannot turn metacharacters completely off. If you have a fixed string you have to escape the characters that vim understands as metacharacters.

Upvotes: 2

Related Questions