如何在 VIM 查找/搜索中转义反斜杠和正斜杠? [英] How does one escape backslashes and forward slashes in VIM find/search?
问题描述
例如,如果我想查找并替换为包含反斜杠或正斜杠的字符串,这将如何在 vim 中完成?谢谢!
For instance, if I wanted to a find and replace with strings containing backward or forward slashes, how would this be accomplished in vim? Thank you!
示例查找 &替换为::%s/foo/bar/g
Examples
Find & Replace is: :%s/foo/bar/g
如果我想找到所有出现的 <dog/>
并将其替换为 <cat\>
what if I wanted to find all occurrences of <dog/>
and replace it with <cat\>
推荐答案
与您在 linuxy 程序中的大多数其他地方转义字符的方式相同,使用反斜杠:
Same way you escape characters most anywhere else in linuxy programs, with a backslash:
:%s/<dog\/>/<cat\\>
但请注意,您可以选择不同的分隔符:
But note that you can select a different delimiter instead:
:%s@<doc/>@<cat\\>@
这可以节省您在模式中使用大量斜杠输入所有那些耗时且令人困惑的反斜杠.
This saves you all typing all those time-consuming, confusing backslashes in patterns with a ton of slashes.
来自文档:
代替围绕模式和替换字符串的 /
,您可以使用任何其他单字节字符,但不能使用字母数字字符,\
、"
或 |
.如果您想在搜索中包含 /
,这很有用模式或替换字符串.
Instead of the
/
which surrounds the pattern and replacement string, you can use any other single-byte character, but not an alphanumeric character,\
,"
or|
. This is useful if you want to include a/
in the search pattern or replacement string.
这篇关于如何在 VIM 查找/搜索中转义反斜杠和正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!