Lets suppose I would like to edit my text and to add comments to the column in the text. This is my text:

something-text1  foobar1 something-text1
something-text2  foobar2 something-text2
something-textn  foobarn something-textn

I want to add comments just after the words foobar. I do the following: (|| is cursor):


M-x kmacro-start-macro-or-insert-counter M-x forward-word

;; comment

M-x backward-word M-x next-line M-x kmacro-end-or-call-macro

Now I can repeat this keyboard macro on every line with the word foobar with
M-x kmacro-start-macro-or-insert-counter or C-u kmacro-start-macro-or-insert-counter and apply the command times.

I would like to do the same with rectangle-mark. I would like to make
region with C-x SPC C-u С-n:

something-text1  ||foobar1 something-text1
something-text2  ||foobar2 something-text2
something-textn  ||foobarn something-textn

Then I eval M-x kmacro-end-or-call-macro, but it doesn't work in the way it works with help of above-mentioned command.

How can I apply a keyboard macro to every line that starts with rectangle mark region?

1 Answer 1


I'm not sure if you can do this with rectangular regions, but it's a perfect situation for multiple cursors:

Fundamentals of multiple-cursors


In your case, starting with point before foobar1, call M-x set rectangular-region-anchor, navigate to the next line until you have a point before every foobar. Then forward-word moves all of your cursors to the end of foobarN, and you can enter the text you want.

Lots of options and variants to consider, see the links.

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.