Reputation: 40778
I would like to pass an argument to an Emacs lisp function on the command line. I tried
emacs --no-splash --eval '(setq my-test-var "hello")' -l init.el t.txt
where init.el
is
(message my-test-var)
but no message is shown. I am using Emacs version 24.3 on Ubuntu 14.04.
Upvotes: 2
Views: 2222
Reputation: 73390
Do you want to use --eval
?
If just want a way to perform init file processing after --eval expressions have been evaluated, then I think the question needs re-wording.
However, assuming that's the case, I think emacs-startup-hook
is as good an answer as any.
As Jesse pointed out, --eval expressions are processed after the init file (and also after-init-hook
) have been processed.
emacs-startup-hook
runs after command-line args have been processed, however.
Upvotes: 1
Reputation: 5369
The variables command-line-args
and command-line-args-left
will allow you to access the arguments passed from the command line (see this manual page).
Docstring for command-line-args
:
Args passed by shell to Emacs, as a list of strings. Many arguments are deleted from the list as they are processed.
Docstring for command-line-args-left
:
List of command-line args not yet processed.
Upvotes: 4
Reputation: 1469
The command passed through the --eval
argument seems to be executed only after Emacs is initialized, so you could have this in your init.el file:
(defvar testvar t)
(defun test-function(arg)
(setq testvar arg)
(message testvar))
And initialize Emacs with emacs --eval '(test-function "hello")'
Upvotes: 1