Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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 ")
    (mapc (lambda (filename)
            (find-file filename)
            (call-interactively command))

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.

share|improve this answer

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

share|improve this answer

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .