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).