Emacs Batch Mode: A Quick Dive Into an Overlooked Feature

The command-line option ‘-batch’ causes Emacs to run noninteractively. The idea is that you specify Lisp programs to run; when they are finished, Emacs should exit.

Emacs thus behaves like a noninteractive application program in batch mode.

Why is this useful

One can run lisp scripts from the terminal.

You can also use it to execute scripts, just as one would execute Bash or Python scripts.

One would put this at the top of the file if planning to run the script from Unix shell.

#!/usr/bin/emacs --script

For example, one could create a simple script to create a quick note and execute it with

emacs --batch -l .\quick-note.el

Sample Script

;; quick-note.el

;; Generate a file name based on the date
(setq filename (format "quick-note-%s.txt" (format-time-string "%Y%m%d-%H%M%S")))

;; Create or open a file with the generated name
(find-file filename)

;; Save the file
(save-buffer)

;; Close the file
(kill-buffer)

Useful for automating tasks on a large numbers of files.

I find it most useful for testing one's init file to see if it will load, following any changes.

So, it is great way to test your dot emacs to see what could be wrong with it.

emacs -batch -l ~/.emacs.d/init.el --eval '(message "init.el loaded successfully!")'

Return to Home