Emacs Lisp: Run Emacs Lisp Code in Shell

By Xah Lee. Date: . Last updated: .

You can run emacs lisp script in shell (terminal), using the --script option. For example:

emacs --script abc.el

where abc.el is file of emacs lisp code.

Here's a table of most useful options for running emacs lisp as a script.

--no-init-file or -q
Do not load Emacs Init File, nor site-wide default.el.
--no-site-file
Do not load the site-wide init file site-start.el. (info "(elisp) Init File")
--load=path or -l path
Run the elisp file at path.
--batch
Do not launch emacs as a editor. Use it together with --load to specify a lisp file. This implies --no-init-file but not --no-site-file.
--script path
Run emacs like --batch with --load set to path.

For a complete list, see:

Prepare Your Emacs Lisp Script to Run in Batch Mode

When you write a elisp script to run in batch, make sure your elisp file is:

  1. self-contained; Doesn't depend on anything from your emacs init file.
  2. Explicitly load all libraries it needs (using require or load).
  3. Has necessary load path set in the script (For example, (add-to-list 'load-path lib_path)) if it needs libs that's not part of standard GNU emacs install, just like you would with a Python or Ruby script.

How to start emacs on a Mac from command line?

If you are on a Mac, call it from the command line like this:

/Applications/Emacs.app/Contents/MacOS/Emacs --script=name.el

Practical Elisp ⭐

Writing Command

Text Processing

Get User Input

File/Buffer

Writing Script