How does one escape backslashes and forward slashes in VIM find/search?

stormist picture stormist · Mar 17, 2010 · Viewed 82.2k times · Source

For instance, if I wanted to a find and replace with strings containing backward or forward slashes, how would this be accomplished in vim? Thank you!

Examples Find & Replace is: :%s/foo/bar/g

what if I wanted to find all occurrences of <dog/> and replace it with <cat\>

Answer

Cascabel picture Cascabel · Mar 17, 2010

Same way you escape characters most anywhere else in linuxy programs, with a backslash:

:%s/<dog\/>/<cat\\>

But note that you can select a different delimiter instead:

:%s@<doc/>@<cat\\>@

This saves you all typing all those time-consuming, confusing backslashes in patterns with a ton of slashes.

From the documentation:

Instead of the / which surrounds the pattern and replacement string, you can use any other single-byte character, but not an alphanumeric character, \, " or |. This is useful if you want to include a / in the search pattern or replacement string.