0

I want to record a macro, which generates a formatted text that includes a number. I want the number to be counted up whenever the macro is invoked.

For example:

abc 1 def
abc 2 def
abc 3 def

Can I do that?

Vim's version is 7.0. OS is CentOS 6.

5

Use <c-a> mapping will increment a number in your macro.

qqyyp<c-a>q@q

The idea is to duplicate the current line (yy and p) and then increment the newly duplicated line (<c-a>).

For more help see:

:h q
:h CTRL-A
:h @
| improve this answer | |
0

I do something similar in a function to number a list but taking note that the register may change between sessions something like

:let @a=`number you want eg 10`

qp "Record macro
:let @a=@a+1

Either

:.put a "puts it current line

or

i   "insert mode
Ctrl-R a  "Control R a will paste register a

q " finish macro
| improve this answer | |

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.