How can i add the same string to many lines ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How can i add the same string to many lines ?

Antharia Jack
Hello everyone ?

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.

For now i just use the dot command to each line.

Thanks for reading.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Grant Taylor-2
On 08/30/2018 10:01 AM, Antharia Jack wrote:
> Hello everyone ?

Hi,

> I would like to add target="_blank" to all my href in a html file.

Okay.  That seems odd, but you do your own thing.

> I would like to say to vim : add this string after the second " symbol
> of each line where is the word href.

Can you provide an example?

I'd be inclined to try a regular expression like the following.

:%s/<a href=\(.\{-1,}\)>/<a href=\1 target="_blank">/g

Use a non-greedy wildcard match to find an anchor tag (assuming it
starts with href) and append the target="_blank" to the end.

> For now i just use the dot command to each line.

That's annoying, especially if you have a LOT of anchor tags to change.

> Thanks for reading.

:-)



--
Grant. . . .
unix || die

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Tim Chase
In reply to this post by Antharia Jack
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.