Thursday, August 30, 2018

Re: How can i add the same string to many lines ?

On 2018-08-30 09:01, Antharia Jack wrote:
> I would like to add target="_blank" to all my href in a html file.
> I would like to say to vim : add this string after the second "
> symbol of each line where is the word href.

If all hrefs are quoted and use the same quotation, it's pretty
straightforward:

:%s/href="[^"*"/& target="_blank"/g

However it becomes more complex if you have other complications like
a mix of single- and double-quotes or unquoted values; or href="..."
in something other than an <a> tag that you want to leave alone.

For a fairly robust implementation, assuming all hrefs are quoted, one
can do

:%s/<a\_s\+href=\("[^"]*"\|'[^']*'\|\s\+\)\zs/ target="_blank"/g

Still has issues if they cross lines or you have other attributes
between the <a> and the href= attribute, like

<a style=foo href=blah>

or

<a
href=blah
>

but should simplify the work you need to do.

-tim

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: