How to include forward slash in vi search & replace

SearchVimReplaceVi

Search Problem Overview


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?

Search Solutions


Solution 1 - Search

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1578447View Question on Stackoverflow
Solution 1 - Searchpb2qView Answer on Stackoverflow