1

I want to create a file that has X number of entries where each entry has incremental numbers for Y. I tried recording a macro that would copy the 3 lines then edit the value but it ended up editing the wrong lines. Is a vim macro the correct way of doing this or is there another special way to duplicate lines and increment their numbers?

I realize I could just write a simple programming script that would print out this but I was already editing the file in vim and thought vim is powerful I should figure this out.

Values for
Y = 1-30

entry 100/1/Y
  fields value Y
  other section Y is valid

Results would be a multiline file that is as followed.

entry 100/1/1
  fields value 1
  other section 1 is valid
...
entry 100/1/30
  fields value 30
  other section 30 is valid
4

A macro is certainly capable of doing this. Starting with the first three lines as a template

entry 100/1/1
  fields value 1
  other section 1 is valid 

Place your cursor on the first line and execute:

qqy2EnterjjoEscp$CtrlajCtrlajCtrlakk0q

  • qq Start a macro in register q
  • y2Enter Yank three lines
  • jjoEscp Copy the lines below inserting an extra blank line in-between.
  • $CtrlajCtrlajCtrla Using vim's auto-increment function, increment the three numbers.
  • kk0 Set up the cursor for the next macro.
  • q Stop recording the macro.

Now you can symply do @q however many times you want to repeat the macro. Or you can also do 30@q to repeat it 30 times.

  • That's what I was missing the Ctrl-A feature. Thank's that's a really handy feature to know. – LF4 Apr 26 '17 at 22:52

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.