1

Is something like the following possible? Transform this

apples
bananas
pizza
burger
juice
water

To:

<tr>
    <li class="first">apples</li>
    <li>bananas</li>
</tr>
<tr>
    <li class="first">pizza</li>
    <li>burger</li>
</tr>
<tr>
    <li class="first">juice</li>
    <li>water</li>
</tr>

I tried this with emmet.io: tr>td.first*1+td*2 but it didn't work. Thanks in advance.

Lukas Kitsche
  • 107
  • 1
  • 1
  • 4

1 Answers1

1

As usual, I'd play with substitute (:h :substitute and groups+references in vim -> :h /\(, :h /\1), however, this one has been tricky because of the multilines issues.

:%s#\(\S*\)\n\(\S*\)\n#<tr>\r  <li class="first">\1</li>\r  <li>\2\</li>\r</tr>\r

Note:

  • the use of \n in the regex part and of \r in the replacement part are not innocent. They are the only way to represent end-of-line in their respective parts when using Vim -- don't ask me why, though I remember a question on the subject on SO or was it on vi.SE.
  • I have chosen # as delimiter instead of the usual / in order to simplify the replacement texts.
Luc Hermitte
  • 29,719
  • 7
  • 60
  • 74
  • Yes, I read the question just today. It was on stackoverflow [here](http://stackoverflow.com/questions/71323/how-to-replace-a-character-by-a-newline-in-vim#71334) – Lukas Kitsche Oct 25 '16 at 14:19
  • Is it also possible with multiple words in one line? Like `apples and bananas\npizza and burger\n juice and water`. I couldn't figure out how this works. – Lukas Kitsche Oct 26 '16 at 07:29
  • What about `^.*$` as the pattern instead of `\S*`? – Luc Hermitte Oct 26 '16 at 07:52