How to avoid backslash and slashes in search / search for VIM?

For example, if I wanted to find and replace strings containing backslashes or slashes, how can I do this in vim? Thank!

Examples Find and replace:: :%s/foo/bar/g

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

+67
vim regex
Mar 17 '10 at 19:14
source share
6 answers

In the same way, you avoid characters the most in Linux programs with a backslash:

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

But note that you can choose a different delimiter instead:

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

This saves you from having to enter all these time-consuming, confusing backslashes in templates with tons of slashes.

From the documentation :

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

+112
Mar 17 '10 at 19:19
source share
 %s:<dog/>:<cat> 

You can replace / delimiters if they annoy certain patterns.

+19
Mar 17 '10 at 19:19
source share

Quote them with a backslash. In addition, it often helps to use a separator other than a slash.

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

or if you should use a slash as a command line delimiter

  :%s/<dog\/>/<cat\\>/ 
+7
Mar 17 '10 at 19:18
source share

Syntax:

 :%s/<dog\/>/<cat\\>/g 
+3
Mar 17 '10 at 19:21
source share

I was looking for something like this to look for register values ​​containing the character / (for macro recording). The solution was to search using a token ? instead of / .

+1
Jul 22 '15 at 20:27
source share

backslash with backslash

 /(<- the prompt)\/\* 

so after entering the type it looks like

 /\/\* 
-one
Mar 10 '16 at 2:48
source share



All Articles