在 VIM 结果中搜索并替换尾随字符
这就是我正在尝试做的事情:
%s/Article/<h2>Article</h2>/gi
不幸的是,每次我通过 vim 编辑器执行此命令时,它都会显示:
尾随字符
为了缓解上述问题,我执行了以下操作:
%s/\s*\r*$//
它执行成功,但是当我返回到原始搜索和替换命令时,它再次读取“尾随字符”并且不执行搜索和替换操作。
我在这里做错了什么?
This is what I am attempting to do:
%s/Article/<h2>Article</h2>/gi
Unfortunately, every time i execute this command through my vim editor, it says:
Trailing characters
To mitigate the above, I executed the following:
%s/\s*\r*$//
And it executes successfully, but when I go back to the original search and replace command, it again reads 'Trailing characters' and does not execute the search and replace operation.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“尾随字符”位于您的命令中,而不是您的文档中。
Vim 认为您已完成
Article 处的命令,然后将
h2>/gi
视为替代命令的第三个参数。但这些字符对于第三个参数并不全部有效,因此它会给出错误。要解决此问题,您需要转义替换中的
/
字符。The "trailing characters" are in your command, not your document.
Vim thinks that you finished the command at
Article</
, then considersh2>/gi
as the third argument of the substitute command. But those characters aren't all valid for the third argument, so it gives you the error.To solve this, you need to escape the
/
character in your substitute.另外,如果您经常需要在正则表达式(XML/HTML/UNIX 文件路径)中使用文字正斜杠,并且不想担心转义每个实例,则可以使用不同的分隔符。例如,使用 !而不是 /:
%s!Article!
Article
!gi
我很懒,这通常比手动转义斜杠更容易。
Also, if you often need to use literal forward slashes in your regexs (XML/HTML/UNIX file paths) and don't want to worry about escaping every instance, you can use a different delimiter. For example, using ! instead of /:
%s!Article!<h2>Article</h2>!gi
I am lazy and this is usually easier than manually escaping slashes.