13

I'm a newbie to emacs. I'm working with emacs-24.1 on redhat linux, and trying to evaluate an elisp expression. What I want emacs to do is to evaluate the elisp expression without launching emacs itself. I'm trying different things

emacs --eval '(+ 2 3)'

I do not know if emacs is evaluating the expression, but the result is not shown on console and emacs window comes up. Next I tried this

emacsclient --eval '(+ 2 3)'

Emacs client is expecting a server. It could not find the server and hence throwing an error (can't find socket. start server etc). So I launched a server (server-name is SERVER) and ran emacsclient again

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

This time, emacs evaluated the expression and printed the result on console. That is because emacs is using the existing server to evaluate the expression. Now I get a problem when the server is not running.

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

This time, I'm not getting any error on console. Emacs is launching a new window, because of -a (my .emacs has (server-start) command in it and server-name set to ANOTHER_SERVER). But emacs then is trying to edit the file (+ 2 3). It is shown on the mode line. I'm confused. emacsclient --help showed me this

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

and emacs manual says this.

'-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 proceed on this. As I said, my goal is to evaluate an elisp expression without launching emacs. Is it possible?

1

2 Answers 2

12

After a bit of testing it looks like you can use --batch to have emacs dump any messages to stderr. Then you can call message to print things to stderr where you'll be able to see them. Your example would become emacs --batch --eval '(message (number-to-string (+ 2 3)))' and the result would be printed to stderr.

If you're trying to redirect the output to a file you'll need to redirect stderr instead of stdout by using 2> instead of just >.

4
  • There may be a better way, I'm pretty new to emacs myself. Jul 13, 2012 at 17:06
  • Why's this not working ? emacs --batch --eval "(message (symbol-value 'user-init-file))"
    – Surya
    Jul 13, 2012 at 17:15
  • Very rough guess but I'm going to assume the environment is a little different when emacs is run in batch mode so user-init-file may not be initialized as it normally would. Are you sure you don't actually want something like M-x ielm in emacs rather than doing this on the command line? You'd get a nice elisp REPL where you can play around instead. Jul 13, 2012 at 17:23
  • However, interestingly (OSX/Emacs 24.1), you can apparently force loading an init file even in batch mode, i.e., this works: emacs -u keith --batch --eval "(print (symbol-value 'user-init-file))" The -u user option will look for and load the specified user's init file (.emacs). Jul 13, 2012 at 17:42
8

Try

emacs --batch --eval '(print (+ 2 3))'
2
  • 2
    Thanks! Up-voted. That command includes a newline preceding the output value. To avoid the newline: emacs --batch --eval '(message "%s" (+ 2 3))' 2>&1 Jul 27, 2014 at 19:19
  • Another way to not have the prepended newline, but still have one appended (which I presume is desirable) would be: emacs --batch --eval '(princ (format-message "%s\n" (+ 2 3)))' ... and if you don't even care about the trailing newline, that can be shortened down to emacs --batch --eval '(princ (+ 2 3))'. With either of these, you don't risk sending actual errors into your output stream, as the 2&1 solution does.
    – lindes
    Oct 29, 2022 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.