How do you apply a macro x amount of times per line where x depends on the line

160 views Asked by At

Say I have something like

a & 1234567890
b & 1234567890
c & 1234567890
d & 1234567890
e & 1234567890
f & 1234567890

Is there a way to use a vim macro such that I can run a macro/command x amount of times per line, where x depends on the line? In this case, I run 2wx^ on each line x times, where x is the line number such that the result becomes

a & 234567890
b & 34567890
c & 4567890
d & 567890
e & 67890
f & 7890

Thanks in advance

1

There are 1 answers

0
VanLaser On

If your macro is recorded in register q, then you can run:

:exec 'normal ' . line('.') . '@q'

on any line you want. Your macro will want the cursor kept on 1st column before being run.

You can also probably do it - better - in a different way, if you describe what you want to do. For example, perhaps you could skip the macro altogether and use something like this instead:

:exec 'normal ^2w' . line('.') . 'xj'

If you need a line offset (e.g. of 1), you could use:

:let nr = line('.') - 1 | execute 'normal ^2w' . nr . 'xj'