Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
 <created>
  pat@c.com 
</created>

I want to replace the above with but the username may vary i.e,pat@c.com ,harry@c.com...

  <created>
   tom@c.com
  </created>

What is the command to replace this in vim

 %s/<created>\r*\r</created>/new string
share|improve this question

1 Answer 1

up vote 0 down vote accepted

It worked for me like this:

%s/<created>\n\s*\w*@\w*\n<\/created>/newstring/

If you wanted to re-insert the e-mail address you can use:

%s/<created>\n\s*\(\w*@\w*\)\n<\/created>/newstring: \1/

A bit of explanation:

  • \n: newline
  • \s: spaces and tabs
  • \w: any word character
  • \( and \): capture what's inside
  • \1: use the first captured expression
share|improve this answer
1  
Try using a different delimiter for clarity, since you have "/" in your pattern: "%s#<created>\n\s*\w*@\w*\n</created>#newstring#". –  BMDan Aug 6 '10 at 13:59
    
Thanks guys,This is the first time i got a reply for a vim editor question. –  Hulk Aug 6 '10 at 14:12
    
Can u explain the above.Not working for me now.. –  Hulk Aug 6 '10 at 14:25
    
@Hulk, updated my answer, how is not working? –  chmeee Aug 10 '10 at 6:49
    
It is now....... –  Hulk Aug 26 '10 at 5:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.