Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

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.
{"cc_re__cc_re__cc_by_sa_3_0": ["sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com?utm_source=stackoverflow.com&amp;utm_medium=site-ui&amp;utm_campaign=footerlink\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a>                   with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.12.27.2141\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Super User works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscri"], "copyright__copyright__copyright": ["g\" flag after a replacement pattern says to operate globally in each line, and not just on the first match in a line.\n\nAll 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.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}