This is a very simplified version of my problem. Suppose I have a text file containing several places the code 20091a or 20092a or 20093a. I'm wishing to surround all this code by the text <code></code>. So the resulting strings above would be like <code>20091a</code> or <code>20092a</code> or <code>20093a</code>.

So how do I replace the search text with text containing the search text? Is there any variable which holds the search text so that I could use something like :%s/2009.a/<code>$SEARCH_TEXT<\/div>?

  • If the original codes are part of an XML file, the more generic way is not to use vi and parse the XML and then insert the text in a newly created node anywhere where you found it.
    – Anthon
    Sep 17, 2014 at 10:01

1 Answer 1


You can use \0 or & in the replacement as in

:%s/2009.a/ \0 /gc
:%s/2009.a/ & /gc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .