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
Related videos on Youtube
Author by
user1578447
Updated on February 25, 2022Comments
-
user1578447 about 2 years
I have a file that contains the string
usrbin
. I want to search forusrbin
and replace it with/usr/bin/
.I tried
:%s/usrbin/usr/bin/g
, but it's showing errorE488: Trailing characters
.How do I include a forward slash in a search and replace?
-
glerYbo about 9 yearspossible duplicate of How does one escape backslashes and forward slashes in VIM find/search?
-
Musa Haidari over 8 yearspossible duplicate of / in vi Search and replace?
-
-
pb2q about 6 yearsthanks @CiroSantilli包子露宪六四事件法轮功! I added edits. Comment if you can think of any other characters that can't be used as alternate delimiters
-
jurgenb over 5 yearsgreat 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 about 4 yearsThere 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 about 2 yearsFYI, the relevant vim help on this topic is
:h pattern-delimiter
.