0

I have a list of cities

London
Madrid
Paris
Brasil

I want to write a macro, that will add a ul tag and wrap each city in to an li.

I think, i need to create a variable like:

let i = 4

Than to write macro a(also have macros b, that will wrap each line into li)

qa(call var i)Vc<ul>j</ul>kp(call var i)@b
2
  • 1
    It seems like what you really want is the ability to use a variable as a count for commands like V and @b?
    – D. Ben Knoble
    Commented Jan 6, 2023 at 15:45
  • Yes, you are write, but how to do that?
    – serii
    Commented Jan 7, 2023 at 8:41

2 Answers 2

0

Doesn't answer the title question, but with https://github.com/tpope/vim-surround, I would do (type literally; angle brackets here are not key notation)

ysip<ul>Vitoj:normal yss<li>

and press Enter.

1
  • I will check a bit later.
    – serii
    Commented Jan 7, 2023 at 8:41
0

I am not sure why you think you need a variable, here.

Assuming the first macro, stored into register b, looks something like:

:let @b = "^C<li>^R"</li>"

Then the second macro, stored in register a, could look like this:

:let @a = "c<ul>^M</ul>^[P:.,']norm @b^M"

with ^M being produced with <C-v><CR>, ^R produced with <C-v><C-r>, and ^[ produced with <C-v><Esc>.

This should let you visually select the lines, say with vip, and do @a:

ul

@b does the following:

  1. move the cursor to the first printable character of the line with ^,
  2. change the rest of the line with C,
  3. insert <li>,
  4. insert the content of the unnamed register with <C-r>",
  5. insert </li>.

@a does the following:

  1. change the currently selected text with c,
  2. insert <ul>, followed by a carriage return, followed by </ul>,
  3. leaves insert mode with <Esc>,
  4. put the lines cut at step 1 above the current line with P,
  5. execute @b on the lines we just changed with :.,']norm @b<CR>.

:help '] is the last line of the last change. Since the last change left you on its first line, you can create a range that covers all the lines in the last change and execute the desired macro on all those lines without having to count them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.