Search code examples
variablesemacsquote

Executing command on all opened buffers


I have opened lots of file in emacs using C-x C-f /path/to/files/*.txt Also I have created and saved a keyboard macro named it as my-macro

Now I want to running this macro on all the opened buffers. How can I do this in emacs?

I would like to mentioned that I am new to emacs and also don't know about emacs lisp code but can code well in C++. So If anyone who suggest or give some lisp code, please favour me by writing the comment also so that I can modified that so any needed.

EDIT 1: I have tried by marking all the opened buffers by m and then E then Eval(form): my-macro But getting the below error

Symbol's value as variable is void: my-macro


Solution

  • The error is giving you a clue: "Symbol's value as variable is void"

    my-macro is not a variable containing a value. You need to quote the macro like that: 'my-macro (or the equivalent (quote my-macro))

    If you really want to execute the macro in all buffers (beware the buffers like *Messages* and so), this would be a programatic alternative

    (defun execute-my-macro-in-all-buffers ()
      (interactive)
      (dolist (buffer (buffer-list))
        (with-current-buffer buffer
          (if (and buffer-file-name
                   (y-or-n-p (format "Execute macro in %s? " buffer-file-name)))
              (execute-kbd-macro 'my-macro)))))