2

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.

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.