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 b
, 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
:

@b
does the following:
- move the cursor to the first printable character of the line with
^
,
- change the rest of the line with
C
,
- insert
<li>
,
- insert the content of the unnamed register with
<C-r>"
,
- insert
</li>
.
@a
does the following:
- change the currently selected text with
c
,
- insert
<ul>
, followed by a carriage return, followed by </ul>
,
- leaves insert mode with
<Esc>
,
- put the lines cut at step 1 above the current line with
P
,
- 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.
V
and@b
?