Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Is there a way implemented in Emacs to apply a function on a set of files? (Or, if not, do you know of such an extension?)

For example, if you have a project in a directory (say, scripts to compile and run, the source, and an XML database). You want to run this function on all those files, one by one:

(defun indent-buffer ()
  "Indent the whole buffer according to indent-region-function."
  (indent-region (point-min) (point-max)) )

What complicates the picture at least to some degree, although certainly not impossible, is that Emacs must be in the correct mode to execute such a function sensibly.

The coolest interface would be to use Emacs as a server, and then send the command, and the file list, as arguments.

Second best if it could be done in Dired.

share|improve this question
For sample code that does similar, look at bytecomp.el implementing the functions byte-compile-file and byte-recompile-directory which performs the following "Recompile every `.el' file in DIRECTORY that needs recompilation." – bsd Nov 11 '12 at 12:34
up vote 2 down vote accepted

In batch mode, loop over the arguments.

emacs --batch -l cl --eval '
  (dolist (filename command-line-args-left)
    (find-file filename)
    (indent-region (point-min) (point-max))

From dired, use the dired-map-over-marks macro from dired.el or the dired-map-over-marks-check function from dired-aux.el.

(dired-map-over-marks-check indent-buffer nil 'indent)
share|improve this answer
The batch mode code works great, only, I had to remove the -1 option, and that's not in my man emacs, either? – Emanuel Berg Nov 12 '12 at 22:40
@EmanuelBerg I have no idea where that -1 crept in from. – Gilles Nov 12 '12 at 23:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.