97

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
  • 58,613
  • 19
  • 146
  • 147
user1578447
  • 1,071
  • 1
  • 8
  • 4
  • 4
    possible duplicate of [How does one escape backslashes and forward slashes in VIM find/search?](http://stackoverflow.com/questions/2465156/how-does-one-escape-backslashes-and-forward-slashes-in-vim-find-search) – kenorb Mar 15 '15 at 13:40
  • possible duplicate of [/ in vi Search and replace?](http://stackoverflow.com/questions/1684628/in-vi-search-and-replace) – Musa Haidari Sep 17 '15 at 12:05

1 Answers1

143

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

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 1
    thanks @CiroSantilli包子露宪六四事件法轮功! I added edits. Comment if you can think of any other characters that can't be used as alternate delimiters – pb2q Apr 21 '18 at 20:28
  • 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 – jurgenb Oct 01 '18 at 11:28
  • 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 – Kajukenbo Mar 12 '20 at 03:32
  • FYI, the relevant vim help on this topic is `:h pattern-delimiter`. – jdhao Feb 24 '22 at 02:16
  • When I try ```:h pattern-delimiter``` I get the error ```E149: Sorry, no help for pattern-delimiter``` – nikost Aug 15 '22 at 20:28
  • @nikost check your vim install, or check on another vim installation. Sounds like your vim install doesn't have all a complete installation. What do you see when you just type `:h`? – pb2q Aug 17 '22 at 14:09
  • I see this as the first line when I type ```:h``` ```help.txt* For Vim version 8.1. Last change: 2019 Jul 21```. I will try updating, I would think this help is older than 2019 – nikost Aug 17 '22 at 15:16