0

How can we run the macro on only the visual selection?

The macro that we want to run is / id: 1,^Mf1s^R=i^M<C-c>:let i += 1^M (where (as given in this SO answer) ^M happens to be the way vim displays 0xD, 0xD is the carriage return character).

What we are doing in the macro is:

Before we begin recording the macro, we set the variable i to 1. We search for pattern id: 1,. Inside the macro, we use the expression register to insert the value stored in i. Then, before we finish recording the macro, we increment the value stored in the variable, which should now contain the value 2.

Now I want to make a visual selection on the text given below and run the macro on it so that id: 1, becomes id: [consecutive numbers].

  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },

  // Visual selection begins here
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  {
    id: 1,
    aid: 1004874871,
    qid: 0,
  },
  // Visual selection ends here

Attempts:

To achieve this I run the command :'<,'>normal @a(assuming that, we recorded the macro in register a) on visually selected text. The result of the command is just not what I expect.

So, I tried without the visual selection by running the command 100@a. The macro after hitting the bottom of the search, loops back and produces unexpected result (at least, here I understand the result produced).

1
  • You might swap C-c for esc, since they are not equivalent.
    – D. Ben Knoble
    Feb 6 at 16:21

1 Answer 1

1

As a macro:

:set nowrapscan
:let @/ = ' id: 1'
:let @i = 1
:let @q = "nf1x\"ip\<C-a>\"iye\<C-x>"
gg@q

(Changing wrapscan is essential to stop when there are no matches; I use the i register to store the count for easy puts and manipulation via the increment and decrement commands.)

But at that point:

:let i = 1
:global/ id: 1/substitute/1/\=i/ | let i += 1

And since :global takes a range, this form is easier to limit to a range, whereas the macro currently always works on all matches.

0

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.