My Vim replace with a regex is throwing a `E488: Trailing characters`

I had the same problem.

Only using other delimiters didn't help. So, additionally

  1. I didn't select any row.

  2. And didn't use g for global.

so just

:%s#to_be_replaced#replacement#

did the job. Changed all occurrences of 'to_be_replaced' with 'replacement'.


If you have this when replacing within a selected block of text, it may be because you mistakenly typed %s when you should only type s

I had this happen by selecting a block, typing : and at the prompt :'<,'>, typing %s/something/other/ resulting in :'<,'>%s/something/other/ when the proper syntax is :'<,'>s/something/other/ without the percent.


I had this issue and couldn't make it go away until I found out that the .vimrc file that I had parts that I copied from else where that contained abbreviations, like this for example:

abbrev gc !php artisan generate:controller

That abbreviation would mess up my search and replace commands which usually look like this:

:%s/foo/bar/gc

by expanding that gc into !php artisan generate:controller, except, that it wouldn't do it on the spot/ in real time. The way that I clued in was by looking through the command history (by pressing : and the up arrow) and seeing

:%s/foo/bar/!php artisan generate:controller

So if you're getting trailing character errors no matter what you do I'd look inside

~/.vimrc

and see if you can find the problem there.


When the separator character (/ in your case) between {pattern} and {string} is contained in one of those, it must be escaped with a \. A trick to avoid that is to use a different separator character, e.g. #:

:%s#@\(\w\+\)#<a href="http://www.twitter.com/\1">\0</a>#gc

PS: If it should do what I think it should do, your pattern is wrong; see my correction.

Tags:

Vim

Regex