I recently found a substitute command for vim where the author had the /
replaced by a !
like this: :%s!foo!bar
and I don't understand the difference with the traditionnal :%s/foo/bar
.
I searched some documentation on this syntax but I didn't find anything relevant, so I tried to experiment by myself and I couldn't figure out clearly the difference between the two forms. Here is what I found:
/
and !
in the same command. E.g: :%s/foo!bar
will fail.!
can be useful with patterns including a /
. For example if I want to replace </
with %
in my file I can do :%s!</!%!g
instead of :%s/<\//%/g
: I don't need to escape /
in the first command, but I'd be surprised if that was the only use of !
./
and work propertly with !
but as I'm far from being a regex master I'm not sure about this point.So my question is: what is the difference between /
and !
in vim substitution and when should I use one instead of the other?
Both can be used as long they are consistent (not mixed). Usually /
is used. But other char can be used if the string you want to replace contains multiple /
. For example :
:%s!/usr/local/bin!/usr/bin!
:%s:/usr/local/bin:/usr/bin:
:%s,/usr/local/bin,/usr/bin,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With