0

Given:

This is nearly identical 
My answer looks like an o
I am glad you did because
Thank you so much, romain

Desired output, just duplicate the line and add 2 at the end:

This is nearly identical 
This is nearly identical 2
My answer looks like an o
My answer looks like an o2
I am glad you did because
I am glad you did because2
Thank you so much, romain
Thank you so much, romain2

I have recorded a macro @q that will start from the first line, duplicate the line (cursor is now at the second line), append 2, and then go to the next line (cursor is now at the third line). The macro works well if I just keep typing @q until the end of the file (never need to move the cursor, cursor movement is handled correctly by the macro).

however if I try to use :'<,'>norm! @q method (seen here), I got this:

This is nearly identical 
This is nearly identical 2
This is nearly identical 22
This is nearly identical 222
This is nearly identical 2222
My answer looks like an o
I am glad you did because
Thank you so much, romain
eliu
  • 2,390
  • 1
  • 17
  • 29
  • As suggested, answered in vi.stackexchange.com: [in cmd mode, 99@q](https://vi.stackexchange.com/a/2984/36673) – eliu Jun 17 '21 at 21:14

2 Answers2

1

Here is your snippet, but with line numbers to help you see the problem:

1 This is nearly identical 
2 My answer looks like an o
3 I am glad you did because
4 Thank you so much, romain

The range is really just a list of line numbers, [1, 2, 3, 4] in this case, which means that your macro is applied on lines 1, 2, 3, and 4.

Since your macro adds a line below the current one, the line previously known as 2 is now 3:

1 This is nearly identical 
2 This is nearly identical 2
3 My answer looks like an o
4 I am glad you did because
5 Thank you so much, romain

and your macro is played on the actual line 2:

1 This is nearly identical 
2 This is nearly identical 2
3 This is nearly identical 22
4 My answer looks like an o
5 I am glad you did because
6 Thank you so much, romain

then on line 3:

1 This is nearly identical 
2 This is nearly identical 2
3 This is nearly identical 22
4 This is nearly identical 222
5 My answer looks like an o
6 I am glad you did because
7 Thank you so much, romain

then on line 4:

1 This is nearly identical 
2 This is nearly identical 2
3 This is nearly identical 22
4 This is nearly identical 222
5 This is nearly identical 2222
6 My answer looks like an o
7 I am glad you did because
8 Thank you so much, romain

In order to avoid that, you must mark every line in the range first. This is done with :help :global:

:'<,'>g/^/norm! @q

where '<,'>g/^/ marks every line in the range and norm! @q executes the macro.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thanks, however, 2 things: [1] my macro has 2 `j`s for going to the third line, but you are suggesting those movements don't have consequences, as the next macro execution won't be where the `j` will end up with, but where the new next line is, right? [2] need a link on explaining `g/^/` – eliu Jun 17 '21 at 21:31
  • 1. Yes. 2. There's already `:help :global` mentioned in the answer, what more do you want? – romainl Jun 17 '21 at 21:35
0

Did you try making the macro, and then 4@q

I tried to recreate your macro, and ran 4@q:

This is nearly identical This is nearly identical 2 My answer looks like an o My answer looks like an o2 I am glad you did because I am glad you did because2 Thank you so much, romain Thank you so much, romain2

This saves having to do a visual selection.

Voortuck
  • 31
  • 1
  • 4