How to include forward slash in vi search & replace

user1578447 picture user1578447 · Aug 6, 2012 · Viewed 72k times · Source

I have a file that contains the string usrbin. I want to search for usrbin and replace it with /usr/bin/.

I tried :%s/usrbin/usr/bin/g, but it's showing error E488: Trailing characters.

How do I include a forward slash in a search and replace?

Answer

pb2q picture pb2q · Aug 6, 2012

Here are two ways:

  • escape the / which is the default substitute separator: :s/usrbin/\/usr\/bin
  • use another substitute separator, e.g., using the hash # character: :s#usrbin#/usr/bin. Note that there are characters that you can't use as a separator: ", \, |