I'm trying to set a variable at load-time:

# emacs --eval '(defvar myvar t)'

Now, in this session, if I C-h v myvar RET I get a nice t. But if I put (message "MYVAR: %s" myvar) in my init file, I get a nasty Symbol's value as variable is void: myvar.

I also tried

# emacs --eval '(eval-after-load (defvar myvar t) t)'

Without luck. (How) can I set (Elisp, not ENV) variables on the command line that I can use in my init file?


This doesn't work because command line options are processed after the init file. This allows the command line to refer to functions and variables defined or loaded in the init file, and allows the init file to declare additional options.

To run something before the init file, you can force a different load order:

# emacs --no-init-file --eval '(defvar myvar t)' --load ~/.emacs.d/init.el
  • --no-init-file / -q: Don't load the init file.
  • --eval ... --load ...: Define the variable and then load the init file.
| improve this answer | |

I found a hackish way: I start Emacs like this:

# emacs -alt

Then in my init file:

(if (member "-alt" command-line-args)
    (let ((default-directory "~/src/elisp-test/"))
      (message "Atlternate conf"))
  (message "Regular conf"))

It works, at the cost of a command-line-1: Unknown option -alt warning in the messages buffer, but it's only for test purposes, so it's OK.

EDIT: @xuchunyang's answer is obviously the right one :

emacs -q --eval '(setq -alt t)' --load ~/.emacs

And in my .emacs:

(defvar -alt nil)

(if -alt
    (let ((default-directory "~/src/elisp-test/"))
      (message "Alternate conf"))
  (message "Regular conf"))
| improve this answer | |

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.