tags:

views:

190

answers:

4

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 occurences of <dog/> and replace it with <cat\>

+3  A: 

Quote them with a backslash. Also, it often helps to use another delimiter besides slash.

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

or if you have to use slash as the substitute command delimiter

 :%s/<dog\/>/<cat\\>/
wallyk
+3  A: 

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.

Jefromi
So it will automatically figure out your chosen delimiter?
stormist
Yes. The character after the `s` is taken to be the delimiter. I can't figure out where I've found a list in the documentation, but I'm pretty sure at least `/:;*%@#` are valid.
Jefromi
Ah, found it. Edited!
Jefromi
+5  A: 
%s:<dog/>:<cat>

You can replace the / delimiters if they become annoying for certain patterns.

Sarah
+1 for `:` delimiter. I didnt know you could use that. I always used `@` in times of 'escape-mess'
jeffjose
+1  A: 

The syntax is:

:%s/<dog\/>/<cat\\>/g
Maurizio Reginelli