8

I'm writing HTML lists, and I want to be able to automagically paste 10 list items where the item text is the only thing that changes.

I could accomplish this using PHP and then just copying the rendered HTML code but if there's a way to skip that step and do it directly from Vim that would save me so much time.

  • 4
    Have a look at the plugin github.com/mattn/emmet-vim, it may suits your need. Otherwise you can use a macro to do so, do you know the text in advance? – nobe4 Jun 3 '16 at 16:43
  • Yes I know what text I want to put in each item. Thank you for the plugin suggestion I'll take a look at home tonight and also look into Vim macros. – Azmodeus Jun 3 '16 at 19:08
9

For what you want to do, emmet-vim, as nobe4 suggested, is your best bet.

In insert mode, li{item $}*10<c-y>, will expand 10 <li>s with item N prefilled as the text. You'll start editing the contents of the first one. While still in insert mode, pressing <c-y>n will move to the next tag for editing.

If your list items are not a sequence repeated text, you can use vim-surround to help.

Example list of text that needs to be wrapped:

abc
def
ghi

If you start with your cursor somewhere in the text, pressing ^vip$<c-v>Stli<cr> will wrap each line with <li></li>.

^     - move to the first column
vip   - select the paragraph
$     - Move to the last column in the selection
<c-v> - Convert the visual selection into a block. Without the
        previous $, it would only select 1 column.
St    - Start vim-surround's tag entry
li    - The tag name
<cr>  - Enter

This can be made into an easy to use key map:

nmap <leader>li ^vip$<c-v>Stli<cr>
  • 1
    You have saved my fingers much agony. Thank you, sir. – Azmodeus Jun 9 '16 at 5:19
  • 1
    @Azmodeus Glad it helped. vim-surround is such a productivity booster that you should take the time to follow along with its docs. And just in case you did't know, emmet-vim will work in pre-compiled stylesheets like sass or less. For example, try this in a scss file: .col$*5>.inner – Tommy A Jun 9 '16 at 20:53
2

I would probably do this with a mapping:

 :nmap ^B o<li></li>^[4hi

The plan is to insert to tags and then place the insert inbetween them, so that you can type in the text, escape back into normal mode and then do ctrl-b again. (PS, the ^B and ^[ are entered using ctrl_V, but you probably knew that already.) To put in ten rows in single command, you can use

10ctrl_b

I was not clear if you have some automagic way of actually creating the item text?

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.