Search code examples
regexvim

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


I'm trying to find all instances of a Twitter handle, and wrap an anchor tag around them.

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

Which gives me:

E488: Trailing characters

Solution

  • 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.