/ in vi Search and replace?

Alternatively you can do :%s,foo/bar/baz,foo/bar/boz,g - I almost never use slashes because of the escaping confusion.


You need to escape the forward slashes internally, too.

:%s/\/Users\/tom\/documents\/pdfs\//<new text>/g

As Sarah suggested, you need to escape ALL forward slashes.

You could instead use another character besides forward-slash as the delimiter. This is handy if your search string has a lot of slashes in it.

:%s#/Users/tom/documents/pdfs/#<new test>#g

This works perfectly in vim. I'm not 100% sure about vanilla vi.


I know this question is several years old, but for others who may land upon this one searching for an easier solution, in 2014, you can substitute the "/" delimiter for something else like "!", as long as you do it in front, middle, and back, like this:

:%s!foo/bar/baz!foo/bar/boz!g

Very simiar to Meder's answer ... But, I find that the exclamation is a lot easier to view as a separator. And I just wanted to confirm that this method still works in the current version of VIM, which I am using in Mac OSX Mavericks.


You can use ? to search

In case of searching pattern in a register, and the pattern contains a '/' character, you can simply use ? command instead of / command from normal mode to start pattern matching. In such case, no more escape required for '/' char. (however you need to escape '?' char now)

? will search in the opposite direction of /, so if you don't mind the search direction, and your search pattern doesn't contains '?' char.

In addition, check the escape() script if you want more.