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.

cul8er
  • 135
  • 1
  • 7

2 Answers2

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 @
Peter Rincker
  • 15,854
  • 1
  • 36
  • 45
1

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
Steve
  • 276
  • 1
  • 13