1

I am pretty new to vim and hence trying to learn some of its powerful features and ofcourse top of the list is macros.

So i am trying to create a pattern like this.

<tag>var 1</tag>
<tag>var 2</tag>
<tag>var 3</tag>
<tag>var 4</tag>
<tag>var 5</tag>
<tag>var 6</tag>
<tag>var 7</tag>
<tag>var 8</tag>
<tag>var 9</tag>
<tag>var 10</tag>
<tag>var 11</tag>
<tag>var 12</tag>
<tag>var 13</tag>
<tag>var 14</tag>
<tag>var 15</tag>
<tag>var 16</tag>
<tag>var 17</tag>
<tag>var 18</tag>
<tag>var 19</tag>
<tag>var 20</tag>
<tag>var 21</tag>
<tag>var 22</tag>
<tag>var 23</tag>
<tag>var 24</tag>

So i created the 1st line manually and then created a macro to do the rest, and i was able to do so, the problem is pressing @@ just creates one line and if i have hold down the @ to keep on creating new lines.

I know this is a little different problem because usually with macros we edit something in a line and then repeat it across multiple existing lines like n@<reg>

However in my case the file is empty and i am creating new lines, so how can i do this. This is what the contents of the register looks like

"q yypfr2l<80><fc>^Haghj

Well what i did was

  1. First yank the current line
  2. Paste it below
  3. Jump to next r which is in var and then move 2 characters left which puts me at the number i want to change
  4. Increment the number by 1
  5. Move to beginning of the line and then go down

So can i repeat this macro so that i can use it like 10@q and so on. Please if i need to change my macro to meet the needs.

2
  • Did you try executing 10@q ? What happened?
    – D. Ben Knoble
    Commented Sep 10, 2018 at 12:42
  • yes.. i did try that.. it just inserts only one line.. no matter what count i prepend the macro with, so 2@q, 10@q, 20@q all just insert one line. Commented Sep 10, 2018 at 13:39

3 Answers 3

1

Christian's answer is of course correct, but I can also explain why your macro won't work without actually looking at the register contents.

In step 5, you move down. If there is no blank line at the end of the file, you cannot move the cursor down, so the macro will fail at this point and playback will stop.

If there is a blank line to move down into, then when you play back the macro, you start by yanking and pasting an empty line. Then you do fr, which will fail, because there is no r to jump to, and the macro playback will stop.

You can fix the macro simply by removing step 5.

I'll note also that step 3 is unnecessary, because the increment command Ctrl-A jumps to the first number after the cursor (on the line) before it increments.

1
  • Thank you very much for observing the mistake in step 5 and that's exactly why the macro is failing, i didn't realize it and step 3, being a beginner i didn't know that i don't really have to move at the number part to increment it. Thanks !! much appreciated. Commented Sep 11, 2018 at 6:23
2

I think your macro is not quite correct. If you look at the output of your recorded output it looks a bit strange.

So I tried to do what you wanted and started with a single line:

<tag>var 1</tag>

Then I recorded the macro like this:

qqyypf l^A0 (e.g. start recording into register q, duplicate the current line yy, paste p, go one character after the space f l, increment, CTRLA, go to start of line 0).

Now I replayed the whole thing 10 times. The result was:

<tag>var 1</tag>
<tag>var 2</tag>
<tag>var 3</tag>
<tag>var 4</tag>
<tag>var 5</tag>
<tag>var 6</tag>
<tag>var 7</tag>
<tag>var 8</tag>
<tag>var 9</tag>
<tag>var 10</tag>
<tag>var 11</tag>
<tag>var 12</tag>

So everything as expected.

I found the following part in your mapping suspicious:

  • <80><fc>^H that is most likely a (special) key pressed (since those begin with <80>) and corrected by using a backspace.
  • aghj (pressing a, then gh which starts select mode, and then j does not seem to make much sense)

BTW: you can simply initialize your macro register (in this case q) by using e.g.

:let @q="yypf l\<C-A>0"

so you don't have to re-record it, if you made a mistake. By similar means, you can correct your register:

:let @q="CtrlRq"

Which meams, enter the content of the Q register after the = by using <C-R> on the commandline (see also the help c_CTRL-R). Then you can move the cursor and correct any wrong key.

2
  • Thank you very much for your help, indeed there were some wrong keys pressed while recording the macro. Also though i was aware of accessing and editing the register contents but never really used it, this makes the perfect use case. Thank you much appreciated. Commented Sep 11, 2018 at 6:29
  • It's never occurred to me to use Ctrl-R to edit a macro. Way nicer than pasting into the buffer. Great tip!
    – Rich
    Commented Sep 11, 2018 at 8:57
1

You can also achieve this goal without a macro by using gCtrl-A (See :help v_g_CTRL-A):

yy23pShift-VGgCtrl-A

This yanks the line, pastes 23 copies, visually selects to the end of the file, and then increments the numbers in the way you desire.

2
  • +1 for using g <Ctrl-A>. That's a relative recent addition though. Commented Sep 10, 2018 at 19:33
  • Wow, this is really nice, i didn't know g<Ctrl-A> exists as well :) . Thanks, this is really useful tip. Commented Sep 11, 2018 at 6:36

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.