Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search and replace for forward slash

Tags:

vim

I am trying to replace multiple forward slashes "//" with a single slash "/".

How would you do that?

Also, How would you replace "asd/qwe/zxc" with "fgh/vbn"?

I was able to do this half way using below. But how do I use forward slash in the search string or the replace string.

:%s/asd.qwe.zxc/fgh/g
like image 957
Neerav Avatar asked Dec 17 '12 19:12

Neerav


2 Answers

Either escape it or use different delimiters.

:s/\/\//\//g
:s#//#/#g

I prefer the latter.

Missed the second part:

:s/asd\/qwe\/zxc/fgh\/vbn/g
:s@asd/qwe/zxc@fgh/vbn@g

You can pick any delimiter that you want in the same manner that you could in ed or sed.

like image 71
D.Shawley Avatar answered Nov 02 '22 05:11

D.Shawley


You can try using:

:%s/\/\//\//g

to replace all double slashes with single slashes (though I imagine a guru will show a much cooler way shortly :) ). The general idea is that you need to escape the slashes.

like image 32
RocketDonkey Avatar answered Nov 02 '22 04:11

RocketDonkey