5

I have several files marked in dired-mode, and I'd like to run a command on (say (delete-trailing-whitespace) on each of them.

Is there a built-in way to do this, or do I need to write it myself?

Basically I want to do something like (dired-do-shell-command) but I want to eval an emacs expression rather than a shell command. I want to do this within emacs on the files I have marked, so I can't use -batch.

8

I wrote something to do what I want, in case anyone else finds it useful:

Update: Updated this solution with a more general purpose command.

(defun mrc-dired-do-command (command)
  "Run COMMAND on marked files. Any files not already open will be opened.
After this command has been run, any buffers it's modified will remain
open and unsaved."
  (interactive "CRun on marked files M-x ")
  (save-window-excursion
    (mapc (lambda (filename)
            (find-file filename)
            (call-interactively command))
          (dired-get-marked-files))))

Now M-x mrc-dired-do-command delete-trailing-whitespace does what I want.

I would be delighted if someone would point out to me that I didn't have to do this, and I overlooked an obvious command like dired-do-command.

4

AFAIK dired does not have such a facility, but ibuffer does — use dired to mark all the files you want to effect (m), visit them (C-u F), run ibuffer (M-x ibuffer [RET]), mark all the buffers (m) and invoke ibuffer-do-eval (E), insert the expression you want to evaluate in every buffer, save them all (S) and close them (D).

1

This isn't quite what you asked for, but you could mark buffers in ibuffer and use 'E' (ibuffer-do-eval FORM)

I know that eshell allows you to use elisp for shell scripting (sort of) so you could probably do it more easily from eshell.

  • Thanks shapr, that's quite useful, though I did want to do it in dired. – Matt Curtis Aug 27 '10 at 11:42
1

The solution of Matt is useful, but it is a bit limited in that it allows only to execute single commands without any arguments. I wondered whether it would be possible to execute macros on marked files, and came up with the following dired command that allows to replay any (saved) macro an arbitrary number of times for each selected file.

With this command, I am finally able to semi-automatically edit many files in rather non-trivial ways!

Update: the new version allows to execute arbitrary commands (including macros) and lisp expressions.

;; 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)))))

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.