Elisp: Run Elisp Script 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 process_log.el

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

Option Full SyntaxOption Short SyntaxMeaning
--no-init-file-qDo not load your init files {~/.emacs, ~/.emacs.el, ~/.emacs.d/init.el} nor site-wide default.el.
--no-site-fileDo not load the site-wide site-start.el.
--batchDo 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.
--load="path"-l pathExecute the elisp file at path.
--script pathRun emacs like --batch with --load set to path.

For a complete list, see:

What's site-start.el?

The site-start.el is a init file for site-wide running of emacs.

It pretty much means a init file for all users of this emacs installation. It may be added by a sys admin, or it may be part of a particular emacs distribution (For example, Carbon Emacs, Aquamacs Emacs, etc.).

You can usually find this file in the directory where emacs is installed, if it exists. Typically in a directory named “site-lisp”. For example, ~/apps/emacs-25.1/site-lisp/. But the file can be anywhere in emacs load-path.

(info "(elisp) Init File")

(info "(elisp) Library Search")

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

Elisp, Writing Scritpts

  1. Run Elisp Script in Shell
  2. Get Command Line Args
  3. Read File
  4. Read File Content as List
  5. Write File
  6. Print, Output
  7. Format String
  8. Walk Dir
  9. Call Shell Command
  10. Get Script Name at Run Time
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.