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 [email protected] with [email protected]
:%s/pat@c\.com/[email protected]/g
With context
:%s:<created>\s*\n\s*\zs.\{-}\ze@c\.com\s*\n\s*</created>:tom
By way of explanation:
:%s:XXX:YYY - Substitute XXX with YYY over the whole file (using colons as delimiters to avoid having to escape slashes or @s with a backslash)
where XXX is:
<created> - literal text to search for
\s* - soak up any spaces or tabs to the end of the line
\n - a new line character
\s* - soak up any spaces or tabs at the start of the line
\zs - special code that says "the match starts here", so only the bit between this and the \ze are actually replaced
.\{-} - catch any characters, as few as possible - this will match 'pat' in the example above
\ze - end of the bit we're changing
@c - literal text - the @ and the start of the domain name
\. - '.' means any character, so to match a literal dot, you must escape it
com - literal text - the end of the email address
\s* - any spaces/tabs to the end of the line
\n - a new line character
\s* - any spaces/tabs
</created> - literal match on the terminator (works because we don't use '/' as the delimiters)
and YYY is just the literal string "tom" to insert
An alternative formation:
:%s:<created>\_s*\zs\S\+\ze\_s*</created>:[email protected]
:%s:XXX:YYY: - as before
where XXX is:
<created> - literal text to search for
\_s* - search for zero or more white-space characters, including new-lines (hence the underscore)
\zs - as before, this is the start of the bit we want to replace (so we're not changing the <created> bit)
\S\+ - one or more non-whitespace characters (\+ is one or more, * is zero or more) - this should catch the whole email address
\ze - as before, the end of the match
\_s* - zero or more white-space characters
</created> - the end delimiter
YYY is then the whole email address.
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:
:help pattern.txt
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:
/<created>\_s*\zs\S\+\ze\_s*<\/created>
Note that I've prefixed the /
with a backslash as the search start is /
. A clever trick with this is that :s
by 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::[email protected]
and 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, :help
is your friend. For example, to find out about \zs
, type:
:help \zs
For information about \_s
, type:
:help \_s
For general information about :s
type:
:help :s
etc...