Saturday, May 14, 2011

Emacs Batch Edit Example

Say the following function is in your .emacs file and lets you indent your C source code to your liking:

(defun c-indent-file (filename)
(interactive)
(find-file filename)
(mark-whole-buffer)
(c-indent-line-or-region)
(save-buffer))
view raw gistfile1.sls hosted with ❤ by GitHub


But you don't want to manually open each file into an Emacs buffer to execute this script. Emacs batch editing to the rescue:

emacs --batch -l '/home/dburger/.emacs' --eval '(c-indent-file "/home/dburger/test.c")'
view raw gistfile1.sh hosted with ❤ by GitHub


The parameters load up my .emacs file as normal and runs the given eval. Apply a little command line magic and you should be able to exploit Emacs from the command line in a very efficient way.