Is it possible to set Emacs start-up command-line flags such as -nw
and -q
in an init file? If it is, how can I do that?
-
Being a non-native English speaker, your corrections are gold to me, @Drew -- thanks! – cig0 Jun 23 at 23:57
You can modify the command line arguments in an init file if you want. But if you do that after Emacs has processed the argument, it won't have any effect.
Emacs processes command line arguments in two places: in the C startup code, and in startup.el
. The C startup code processes arguments before it executes any Lisp code, so there's no way to influence that from an init file. As of Emacs 26.2, the following options are handled in C: --version
, --chdir
, -t
, -nw
, --batch
, --script
, -daemon
, --bg-daemon
, --fg-daemon
, --help
, --no-loadup
, --no-site-lisp
, --no-build-details
, --module-assertions
and (partially) --display
.
So in particular there's no way to completely prevent the initialization of the window system (-nw
) from Lisp.
You can modify other options from a Lisp file that's loaded before startup.el
processes the command line. The main relevant steps are:
- Load
subdirs.el
from directories on the load path. You can control this with the environment variableEMACSLOADPATH
. - Process some command line options (code), including the ones that control what init files to load (e.g.
-Q
,-q
) and a few user interface variables (including-display
). - Finish initializing the window system and create the initial frame.
- Load
site-start.el
(prevented by-Q
) and the user init file (.emacs
or other name, prevented by-q
or-batch
). - Call
package-initialize
. - Process remaining command line options (code), including options that control the appearance of Emacs and options to run code and open files (
-l
,--eval
,--find-file
, …).
-
There're tons of stuff to digest in this answer, thank you for your thorough answer, Gilles. – cig0 Jun 23 at 23:52
Is it possible to set Emacs start-up command-line flags such as
-nw
and-q
in an init file? If it is, how can I do that?
In general, no, you can't do that -- and your second example option is precisely what I would have used to explain why:
-q
(aka --no-init-file
) tells Emacs not to load your init file, so trying to set that option in your init file does not make sense.
That said... some arguments are processed after loading the init file, and so for certain arguments you could populate the command-line-args
variable in your init file to imitate using them on the command line. These options include:
--directory
--eval
--execute
--file
--find-file
--funcall
--insert
--kill
--load
--no-desktop
--no-splash
--visit
e.g.:
(setq command-line-args
(append command-line-args '("--eval"
"(message \"hello\")")))
I would not expect there to be any benefit to jumping through such hoops to use these "Action options" in your init file, as there are more direct ways of performing the actions.
-
Oh.. I meant -Q! I didn't know about
command-line-args
, that's a great piece of advice -- thank you. – cig0 Jun 23 at 23:52 -
1
-