<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
It depends on the formatting that you want, but for the specific example that you quote, you could do one of these:
Really simple, without context: replace pat@c.com with tom@c.com
With context
By way of explanation:
An alternative formation:
I hope that gives you some helpful information. There are lots of useful reference guides on regular expressions (which is what these are) on the web (although note that Vim uses a slightly different format to most:
\+instead of+etc). I’d strongly recommend reading:But bear in mind there’s a lot in there, so read it gradually and experiment. You can also start by using a simple search (press
/) and thinking about doing a substitution later, e.g. type:Note that I’ve prefixed the
/with a backslash as the search start is/. A clever trick with this is that:sby default uses the last search, so you can type the line above (/<created>\_s*\zs\S\+\ze\_s*<\/created>) and tweak it until it’s right, then just do:%s::tom@c.comand since the bit marked XXX above is absent, it’ll use your last search and just work!If there are any bits above you don’t understand,
:helpis your friend. For example, to find out about\zs, type:For information about
\_s, type:For general information about
:stype:etc…