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.

2 Answers 2


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


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 @

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


:.put a "puts it current line


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

q " finish macro

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.