Execute vim substitute command that includes '/' characters

给你一囗甜甜゛ 提交于 2019-12-20 05:16:06

问题


I have a java file in which I wrote /t, instead of \t, the proper notation of the tab escape function. I want to use vim’s substitution feature (:s) but when I do something like this:

:%s//t/\t/g

I get the error message E488: Trailing characters

How can I write the command such that I can execute the substitution? Thank you.


回答1:


Use backslash to escape the characters. So:

:%s/\/t/\\t\g

You can also use a different delimiter like:

:%s@/t@\\t@g



回答2:


Use colons instead of a slash to avoid confusions and errors if what you want to look-up/replace has slashes. This way VIM will be able to parse the sentence correctly.
Then you need to scape the back-slash otherwise VIM will replace /t with tabulations

:%s:/t:\\t:g



回答3:


Escape the forward slash in your search pattern:

:%s/\/t/\\t/g

or use another separator:

:%s;/t;\\t;g


来源:https://stackoverflow.com/questions/20956309/execute-vim-substitute-command-that-includes-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!