1
votes

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.

3

3 Answers

3
votes

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.

1
votes

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")'

1
votes

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.