I have a large html file in which I need to search and replace over 2000 occurrences of specific string. I could do it manually but I'm sure there is a power Vim search and replace command that could do it.

I have footnotes that I need to replace a specific portion of the hyperlink text.

The footnote link is formatted liked this:

<sup><a href='#footnote-2020'>2020</a></sup>

There are over 2000 footnotes each labelled 1-2020. I would like to remove the anchor text (2020 in the example above) and replace it with a *. I would like to leave the href text '#footnote-2020' as it is in order to preserve the link.

Are there any power Vim users who could come up with a quick search and replace command?


  • I can't help you with Vim, but would like an Emacs solution :-P. Or maybe sed? – wingedsubmariner Sep 3 '13 at 2:07

Try this:

:%s,\(<a href='#footnote-\d\+'>\)\d\+\(</a>\),\1*\2,g

To break it down:

% - Operate over the whole file (this is a special range)
,...,..., - Normally the delimiter for :substitute is /, but it can be anything. I used commas because of the </a> tag.
\(...\) - Create a sub-expression, for use in the replacement pattern. Two of these are used.
\d\+ - Match any digit, and the \+ makes it match one or more.
\1 / \2 - In the replace pattern, gets substituted with the first and second sub-expression mentioned earlier.
g - The "g" flag after a replacement pattern says to operate globally in each line, and not just on the first match in a line.

All this and more is explained in :help pattern.txt but that might be overwhelming. There is a book called "Mastering Regular Expressions" that can be helpful.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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