Reputation: 11774
I'm doing a simple search and replace command in vim and I'm trying to escape the <>
and /
characters so I can use them in my search expression. I realize there are more elegant ways of finding HTML tags, but what I'm really looking for is a localized search and replace command that will replace <h1>UNKNOWN MISSION</h2>
with <h2>UNKNOWN MISSION</h2>
. Here's my command where I try to escape the special characters:
:%s/\<h1\>UNKNOWN MISSION\<\/h2\>/\<h2\>UNKNOWN MISSION\<\/h2\>/
The pattern is not matching. Any ideas?
Upvotes: 1
Views: 1969
Reputation: 196546
I'd do the following…
Place the cursor on the first instance of UNKNOWN MISSION
with:
/<h1<CR>
Yank until 2
with:
yt2
Do the substitution with:
:%s+<C-r>"2>+<C-r>"1>
where <C-r>"
is used to insert the content of the default register in the command line and +
is used as an alternative separator in order to avoid escaping slashes. After insertion, the whole command looks like:
%s+<h1>UNKNOWN MISSION</h2>+<h1>UNKNOWN MISSION</h1>
Add a /g
flag if you need it.
Upvotes: 1
Reputation: 8937
:%s/<h1>UNKNOWN MISSION<\/h2>/<h2>UNKNOWN MISSION<\/h2>/g
Seems to be working for me. You only have to escape the backslash.
Upvotes: 3
Reputation: 208475
You don't need to escape the <>
characters, try the following:
:%s/<h1>UNKNOWN MISSION<\/h2>/<h2>UNKNOWN MISSION<\/h2>/
Upvotes: 1