例如,如果我想查找并替换为包含反斜杠或正斜杠的字符串,在 vim 中如何实现?
示例
查找并替换为::%s/foo/bar/g
如果我想查找所有出现的 <dog/>
该怎么办?并将其替换为 <cat\>
最佳答案
与在 linuxy 程序中大多数其他地方转义字符的方式相同,使用反斜杠:
:%s/<dog\/>/<cat\\>
但请注意,您可以选择不同的分隔符:
:%s@<doc/>@<cat\\>@
这可以节省您在带有大量斜杠的模式中键入所有那些耗时且令人困惑的反斜杠。
来自documentation :
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.
关于regex - 如何在 VIM 查找/搜索中转义反斜杠和正斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2465156/