How to include forward slash in vi search & replace

82,995

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: ", \, |

You can review this in the help subsystem using :h pattern-delimiter

Share:
82,995

Related videos on Youtube

user1578447
Author by

user1578447

Updated on February 25, 2022

Comments

  • user1578447
    user1578447 about 2 years

    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?

  • pb2q
    pb2q about 6 years
    thanks @CiroSantilli包子露宪六四事件法轮功! I added edits. Comment if you can think of any other characters that can't be used as alternate delimiters
  • jurgenb
    jurgenb over 5 years
    great answer, this is ideal if you want to replace a long URL value because escaping all those forward slashes is no fun at all
  • Kajukenbo
    Kajukenbo about 4 years
    There are problems with | (pipe) as the delimiter when replacing backslashes such as "PATH=C:\Windows\System32;C:\Temp;C:\Users" via :s|\\|/|g but I never figured out why so I just use # as a delimiter instead
  • jdhao
    jdhao about 2 years
    FYI, the relevant vim help on this topic is :h pattern-delimiter.