4

How can I pass arguments -q -d -Q -t -L -fg -bg --color etc?

Doing something like emacs --script -Q <scriptname> <arguments> definetely will not pass arguments, which are used in emacs. So how to do it?

4 Answers 4

5

Based on your comment on Rafael Ibraim's answer, I'm adding this second answer, as I think my first answer is also mis-interpreting your question (and if so, you may wish to edit the question to provide clarification).

You can prevent Emacs from processing command line arguments using the usual approach of an 'empty' argument: --

So if you run this:

emacs --script (filename) -- -Q

Emacs will not eat the -Q argument (or the -- in fact), and it will be available to your script. You can easily verify this with the following script:

(print argv)
3
  • Thank you! Thank you! That was exactly what I wanted. And now i wrote ":"; exec emacs --no-site-file --script "$0" -- "$@" # -*-emacs-lisp-*- so this thing doing it's work perfectly. Many thanks again. Commented Jul 26, 2011 at 19:37
  • It turns out that you need both this and Jurijs Oniscuks' answer. -- is needed to prevent Emacs from processing option arguments before the script executes, and (setq argv nil) is needed before the script exits to prevent Emacs from processing non-option arguments as filenames, and visiting them.
    – phils
    Commented Jun 23, 2014 at 18:05
  • Actually, even better than purging argv is to simply exit explicitly at the end of the script with (kill-emacs 0) (assuming 0 is the appropriate status at that point), which ensures (a) that Emacs doesn't do anything else (processing of remaining arguments, or otherwise); and (b) that you guarantee which exit status your script is returning (which is something you should want to do in any case). Refer to lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
    – phils
    Commented Sep 5, 2015 at 3:35
2

If you run emacs in script mode, I would recommend to reset "argv" variable to nil in the end of your script, otherwise emacs will try interpret "argv" after script is finished.

Let's assuming you have a file named "test-emacs-script.el" with the following content:

#!/usr/bin/emacs --script
(print argv)
(setq argv nil)

Try running this script as "./test-emacs-script.el -a". If you run this script without resettings "argv" (last line in the script) then the output will be:

("-a")
Unknown option `-a'

Resetting "argv" gets rid of "unknown option" error message

4
  • The problem here is that Emacs will still eat arguments it recognises before they get to your script. For instance, the output for your example with ./test-emacs-script.el -Q is nil. The trick of using the -- argument avoids that issue as well as the one you are addressing, so I believe your approach is only recommended if you don't actually want those arguments to reach your script.
    – phils
    Commented Oct 15, 2011 at 0:58
  • It turns out that you need both approaches. -- is needed to prevent Emacs from processing option arguments before the script executes, and (setq argv nil) is needed before the script exits to prevent Emacs from processing non-option arguments as filenames, and visiting them.
    – phils
    Commented Jun 23, 2014 at 18:03
  • Actually, even better than purging argv is to simply exit explicitly at the end of the script with (kill-emacs 0) (assuming 0 is the appropriate status at that point), which ensures (a) that Emacs doesn't do anything else (processing of remaining arguments, or otherwise); and (b) that you guarantee which exit status your script is returning (which is something you should want to do in any case). Refer to lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
    – phils
    Commented Sep 5, 2015 at 3:36
  • This doesn't seem to be true anymore in 2021 running Emacs 28. Commented Sep 4, 2021 at 3:18
1

I think you have two options here:

  • Create your own argument like explained here.
  • Use the variable command-line-args. The only problem is that this variable probably doesn't do exactly what you want(from the manual):

Most options specify how to initialize Emacs, or set parameters for the Emacs session. We call them initial options. A few options specify things to do, such as loading libraries or calling Lisp functions. These are called action options. These and file names together are called action arguments. The action arguments are stored as a list of strings in the variable command-line-args. (Actually, when Emacs starts up, command-line-args contains all the arguments passed from the command line; during initialization, the initial arguments are removed from this list when they are processed, leaving only the action arguments.)

In other words, only action arguments will be available in command-line-args, wich doesn't really help you much.

1
  • I am not interesting in passing arguments to Emacs. I am interesting in NOT passing it to emacs, but to emacs script. And when emacs script is called with "emacs --script <scriptname> <arguments>", and <argument> is something, what is used in emacs (--color, for example), script don't work as expected(expected to pass --color to script). Commented Jul 23, 2011 at 21:39
1

If you're typing the command manually at the shell there shouldn't be a problem (excepting that in your example your script name is -Q), so I assume you're trying to create an executable script with emacs as the shebang command?

This is the best solution I've seen for creating portable executable elisp scripts which include additional arguments:

Emacs shell scripts - how to put initial options into the script?

Your Answer

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

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