Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

vim

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 
like image 777
Brian Dant Avatar asked Apr 05 '13 19:04

Brian Dant


2 Answers

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.

like image 62
Ingo Karkat Avatar answered Sep 18 '22 18:09

Ingo Karkat


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.

like image 40
Kyle Heironimus Avatar answered Sep 18 '22 18:09

Kyle Heironimus