Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

linux - Search and replace in VIM results in trailing characters

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*
*$//

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?

question from:https://stackoverflow.com/questions/6714469/search-and-replace-in-vim-results-in-trailing-characters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The "trailing characters" are in your command, not your document.

Vim thinks that you finished the command at Article</, then considers h2>/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.

%s/Article/<h2>Article</h2>/gi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...