Evaluate emacs lisp expression on command line

I am new to emacs. I am working with emacs-24.1 on redhat linux and trying to evaluate an elisp expression. What I want emacs to do is evaluate the elisp expression without running emacs. I try different things

emacs --eval '(+ 2 3)' 

I do not know if emacs evaluates an expression, but the result is not displayed on the console and the emacs window appears. Then i tried this

 emacsclient --eval '(+ 2 3)' 

Emacs client is waiting for the server. He could not find the server and, therefore, threw an error (cannot find the socket, start the server, etc.). So I started the server (SERVER server name) and started emacsclient again

 emacsclient --server-file=SERVER -e '(+ 2 3)' 

This time emacs checked the expression and printed the result on the console. This is because emacs uses an existing server to evaluate the expression. Now I have a problem when the server is not running.

 emacsclient --server-file=ANOTHER_SERVER -e '(+ 2 3)' -a emacs 

This time I get no errors on the console. Emacs launches a new window because of -a (my .emacs has (server-start) command in it and server-name set to ANOTHER_SERVER). But then emacs tries to edit the file (+ 2 3). It is displayed on the line mode. I'm confused. emacsclient --help showed me this

 -e, --eval Evaluate the FILE arguments as ELisp expressions 

and the emacs manual talks about it.

 '-e' '--eval' Tell Emacs to evaluate some Emacs Lisp code, instead of visiting some files. When this option is given, the arguments to emacsclient are interpreted as a list of expressions to evaluate, not as a list of files to visit. 

I do not know how to do that. As I said, my goal is to evaluate the elisp expression without running emacs. Is it possible?

+8
emacs elisp emacsclient
source share
2 answers

After a little testing, it looks like you can use --batch so that emacs --batch any messages to stderr. You can then call message to print things in stderr, where you can see them. Your example will be emacs --batch --eval '(message (number-to-string (+ 2 3)))' , and the result will be printed on stderr.

If you are trying to redirect the output to a file, you will need to redirect stderr instead of stdout, using 2> instead of > .

+9
source share

Try

 emacs --batch --eval '(print (+ 2 3))' 
+8
source share

All Articles