I have a named-and-saved keyboard macro that I want to apply to all regular expression matches. How to do that?

(Related: I found this stackoverflow post: Running a macro till the end of text file in Emacs. It kind of works for now, but it is not satisfactory for the following reasons:

  1. Not aesthetically pleasing: why run for all lines when you want to run for all matches?
  2. Error messages: because the macro is supposed to be run for all matches and not for all lines.)
  • Removed tag elisp-macros. Elisp macros have nothing to do with keyboard macros.
    – Drew
    Commented Jun 15, 2021 at 4:12

1 Answer 1


You can do that simply by recording the search for that regexp as the first part of your macro and using M-0<f4> to execute the macro -- per the Q&A you linked to, a prefix arg of zero repeats the macro indefinitely until you type C-g or an error occurs (which is what will happen when the search fails). Refer also to C-hig (emacs)Basic Keyboard Macro.

This doesn't avoid triggering an error (and indeed it's necessary), but this is the normal way of repeating a macro as many times as needed. Unless that's causing you a genuine problem (how?) then my advice is to just accept it.

Failing that, you can do what you want in elisp. Depending on how the named macro has been stored you should be able to use one of the following:

(while (re-search-forward REGEXP nil t)


(while (re-search-forward REGEXP nil t)
  (execute-kbd-macro 'your-macro-name))

You wouldn't record the search as part of this macro, of course.

  • Yes, that would apply the macro for the first regexp search result. Thereafter I have to apply the macro to remaining matches by pressing F4 or C-x e. A file may have variable number of matches. I want to programmatically apply the macro to all the regexp matches without having to press through F4 or C-x e. Commented Jun 16, 2021 at 3:03
  • I would accept the above as an answer! Commented Jun 16, 2021 at 4:36
  • Unless that's causing you a genuine problem (how?): well I have 6 macros that need be to applied one after another to transform the file to a required format. So I have been trying to see if I could automate that (as it is the same operations every single time). However every time an error is triggered that breaks my attempts at automation. Now I feel I can do this with your elisp code example: (while (re-search-forward REGEXP nil t) (your-macro-name)). Still thought of sharing my situation as you may have some advice on how to run macros one after another, after the last one triggers error. Commented Jun 16, 2021 at 4:44
  • Probably something like (dolist (macro '(m1 m2 m3 m4 m5 m6)) (save-excursion (while (re-search-forward REGEXP nil t) (funcall macro)))) -- or, again, execute-kbd-macro instead of funcall if necessary.
    – phils
    Commented Jun 16, 2021 at 5:44

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.