On 17 Feb 2011, at 16:05, DK wrote:

> This should be straight forward but I can't readily find an answer:
> 
> If I have a search:
> 
> :%s/search_text.*more_text/replace_text/gc
> 
> How can I access the matched text (note above the regex in the
> middle).
> 
> For example I would want to do something like (coming from TextMate I
> could use the $0 value to access the match and use it in the replace):
> 
> :%s/search_text.*more_text/replace_text$0more_replace_text/gc
> 
> Where $0 would be the match and I would be appending to it.

Vim uses '&' as the $0 string in your question. So, 
> :%s/search_text.*more_text/replace_text$0more_replace_text/gc
becomes
> :%s/search_text.*more_text/replace_text&more_replace_text/gc

More generally, if you want to apture parts of a match, use \( & \) to surround 
each part, then you can substitue '\1' (or some other number) to insert the 
matched substring. Fpr example:-

:s/\(abcd\)\(efgh\)/\2\1

reverses the order of the substrings 'abcd' and 'efgh' in the current line.

Regards, Andy

-- 
Andrew Long
andrew dot long at mac dot com





Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to