8

If I have to do some variable name refactoring, I use ag and multiple-cursors.

But when I have to do some code block removal/addition/moving, I use keyboard macros. But I need to switch to each buffer and initiate that macro run manually.

Is there a way to simply selected the open buffers from Ibuffer (or files from dired) and execute the last recorded keyboard macro on all of them?

8

Modify selected Ibuffer buffers

After recording the keyboard macro, switch to Ibuffer and,

  • Mark the desired buffers using m
  • Hit W - Eval while viewing buffer (The plain eval E does NOT work for macro calls)
  • (kmacro-end-or-call-macro 1)
3

You simply include the buffer-switching in the macro. You're not restricted to a single buffer -- a keyboard macro can do anything you can do!

I answered pretty much this same question the other day on S.O., so refer to emacs cross-file keyboard macro.

  • I want to run the macro only in few selected buffers of all open buffers. – Kaushal Modi Dec 5 '14 at 14:31
  • So mark the files you want to run it on and filter the listing to just those files. In both dired and ibuffer you can use t to toggle marks and k to remove the marked entries. A subsequent g to refresh will restore the removed lines. – phils Dec 5 '14 at 15:21
  • 1
    I can simply use M-{ and M-} to jump to next and previous marked items in Ibuffer and dired. – Kaushal Modi Dec 5 '14 at 16:34
  • Ah, even better! – phils Dec 5 '14 at 17:05
1

Here's a command that makes executing commands, macros, and evaluating Lisp expressions in multiple files a no-brainer. Note that the prefix argument allows to execute a macro for each file until an error occurs. I posted this already on superuser but it works so well that I thought people coming to this page could also find it useful. I like to bind this to 'E' in dired.

Update: the old version of this function supported only macros. The updated one is both simpler and more powerful, allowing to execute arbitrary commands (by key sequence or name) as well as Lisp expressions. It is now also careful to log any errors in the Messages buffer.

;; Inspired by M-x edit-kbd-macro and https://superuser.com/q/176627.
(defun my-dired-do-execute (keys &optional arg)
  "Execute a command in all marked files.
If an error occurs, execution in other files is not affected.
(Notably, this allows to run keyboard macros until there is an error.)

At the prompt, type any bound key sequence, or `\\[execute-extended-command]'
to choose a command by its name, or `\\[eval-expression]' to enter a Lisp expression.

The prefix ARG, if given, is passed on to the chosen command.
"
  (interactive
   (list (read-key-sequence (substitute-command-keys "Key sequence to execute, \
or \\[eval-expression], or \\[execute-extended-command]: "))
         current-prefix-arg))
  (when keys
    (let ((cmd (if (arrayp keys) (key-binding keys) keys))
          exp)
      (cond ((eq cmd 'execute-extended-command)
             (setq cmd (read-command "Name of command to execute: "))
             (if (string-equal cmd "")
                 (error "No command name given")))
            ((eq cmd 'eval-expression)
             (setq exp (read--expression "Eval in selected files: "))
             (setq cmd nil))
            ((null cmd)
             (error "Key sequence %s is not defined" (key-description keys))))
      (mapc (lambda (filename)
              (save-selected-window
                (find-file-other-window filename)
                (setq current-prefix-arg arg)
                (condition-case-unless-debug err
                    (if cmd
                        (call-interactively cmd)
                      (message "Result in file %s:" filename)
                      (eval-expression exp))
                  (error (message "In file %s: %S" filename err)))))
            (dired-get-marked-files)))))
0

Modifying selected files in dired

The below solution needs installation of the dired+ package, which is available on Melpa.

  1. First of all, convert your keyboard macro to an elisp function. Here's a related emacs.SE QnA (How to save a keyboard macro as a Lisp function?).

    It's even quicker if you know how you want to modify all the files using an elisp function directly. Let's say you want to add "-*- mode: org -*-" at the beginning of all the selected files in dired.1

    In that case, running the below elisp function in each of the selected files would work.

    (defun my/set-org-mode ()
      (goto-char (point-min))
      (insert "-*- mode: org -*-\n"))
    

    So just evaluate the above function for now; it will come to use at a later step below.

  2. Do M-x dired and open the directory containing the files in which you want to run the above function.
  3. Mark all such files using the m key (bound to dired-mark in dired).
  4. *magic step*: Do C-u @ and select the name of the function you evaluated in Step 1 (my/set-org-mode). @ is bound to diredp-do-apply-function which is defined in dired+.el.
  5. Boom! The "-*- mode: org -*-" line will be added at the top of all the marked files. But things are not saved yet. You can review what all changed if you want to and then hit C-x s ! to save all the modified files in one go.

1: Inspired from this question on reddit.

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.