2

If I have following lines:

1
2
3

I make a macro to insert a new line below current line and type 'x': my macro is:

ox^[j

now I run this macro to the above three line by visual select the lines and type command:

:'<,'>normal @q

it will give me the result:

1
x
x
x
2
3

instead of the expected result:

1
x
2
x
3
x

How can I achieve the expected result?

  • Not a solution to correct your macro but the following command does what you're trying to do '<,'>s/$/\rx (substitute each end of line by a new line \r and an x) – statox Jun 23 '17 at 7:09
2

You probably forgot to have your macro move down a line. So it did add a new x always on the same line. However since you did not share what your macro actually looks like, one cannot tell for sure.

[update]

You seem to be calling your macro from visual mode as you show in your updated article. However, note that it won't work as you expect. This is because your '<,'>norm! @a will position the cursor after each invocation on each consecutive line of the visual mode. However, since each time you run the macro you add one line, the positioning will be off by at least one.

Best way would be to avoid visual mode in this case and call your macro by hand 3@q

  • This is most likely the case. If your macro is ox<Esc>, you should make it ox<Esc>j. – Wouter Donders Jun 23 '17 at 12:01
  • I just tried move one line below, but it produces the same result. – Aaron Shen Jun 23 '17 at 12:52
1

I find this wonderful post about advanced macro.

Turns out if you macro will add new lines, then simply run :'<,'> norm @q won't work.

To solve my problem, you need to take care of 2 things:

  1. As Christian and Wouter suggested in the above answer, you must record your macro moving to the correct line to continue.
  2. prefix macro with number of times to run :norm n@q
0

With the macro ox^]j I got the same result as you, I suppose this is because the cursor isn't moved when using a range commmand.

The work around to still use the visual selection would be

:'<,'>g/./norm @q

This way the macro is executed on each line of the selection.


Also as I suggested on the comments, I would rather use a substitution to do what you want:

:'<,'>s/$/\rx

To substitute each end of line by a new line and an x

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.