2

In VIM, if, for example, I have the text

http://a.com
http://b.com

is it possible to find all lines (the whole line) and replace it with something before and after it, such as:

<a href="http://a.com">http://a.com</a>
<a href="http://b.com">http://b.com</a>

Note that the text from every line is repeated. Once for the href and another for the text.

1
5
:%s:.*:<a href="&">&</a>:

Same as in ed/sed/perl...

Another less ex and more vim-like way would be: if you know how to do it once for a line, record it as a macro and then run :%normal @m where m is that macro.

Like (in normal mode): qmS<a href="<Ctrl-R>""><Ctrl-R>"</a><Esc>q to record the macro.

2
  • Didn't work for me. It gave me this error:E748: No previously used register Press ENTER or type command to continue Feb 3 '16 at 22:28
  • @AlexandreSantos, oops. I had overlooked that | was special. Should work better with : Feb 4 '16 at 4:58
1

why do you use the Pipe?

:%s/.*/\<a href=\"&\"\>&\<\/a\>/g

(mark all command strings with \ )

2
  • You don't need to escape the < or >.
    – muru
    Feb 4 '16 at 5:01
  • 1
    On the right-hand side of s/x/y/, none of <, " or > are special, they don't need to be escaped. / is only special if you use it as the delimiter. g is superfluous. Feb 4 '16 at 5:02

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.