Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I ran the following command from the command line expecting to get 5 (the result of the expression), but instead I got some error message about "symbol's function is not defined".

emacs -e "(+ 2 3)"

So, what did I do wrong? What is the correct way to quickly evaluate an expression without having to really open emacs?

Thanks

share|improve this question

2 Answers 2

What you're looking for is Emacs' batch mode, which instructs Emacs to execute the given Lisp program (whether given by -l FILE, -f FUNCTION, or --eval FORM), and then exit. From the GNU Emacs Lisp Reference Manual:

The command-line option ‘-batch’ causes Emacs to run noninteractively. In this mode, Emacs does not read commands from the terminal, it does not alter the terminal modes, and it does not expect to be outputting to an erasable screen. The idea is that you specify Lisp programs to run; when they are finished, Emacs should exit. The way to specify the programs to run is with ‘-l file’, which loads the library named file, or ‘-f function’, which calls function with no arguments, or ‘--eval form’.

Any Lisp program output that would normally go to the echo area, either using message, or using prin1, etc., with t as the stream, goes instead to Emacs's standard error descriptor when in batch mode. Similarly, input that would normally come from the minibuffer is read from the standard input descriptor. Thus, Emacs behaves much like a noninteractive application program. (The echo area output that Emacs itself normally generates, such as command echoing, is suppressed entirely.)

And for a quick example:

me@host $ emacs -batch --eval '(message (format "1+2 equals %d" (+ 1 2)))'
1+2 equals 3
me@host $ 
share|improve this answer

If you are using GNU Emacs, then the syntax for the command-line option that evaluates a sexp is this (from the Emacs manual, node Action Arguments):

`--eval=EXPRESSION'
`--execute=EXPRESSION'
     Evaluate Lisp expression EXPRESSION.

That is, you can use --eval=... or --execute=.... AFAIK, you cannot use -e....

You can, however, use -e... (or --eval) with emacsclient. E.g.: emacsclient -e (text-mode)

(It is best to specify the exact error message you get, as well as your Emacs version, to get the most help.)

share|improve this answer

Your Answer

 
discard

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.