0

I'd like to configure emacs to enable desktop-save-mode at startup, but only if it is not invoked with a file to edit. That is, I want to use desktop-save-mode when I call emacs, but not when I call emacs file.txt. Originally, I considered just testing if command-line-args was empty, but this would also disable desktop-save-mode if I called emacs with flags for some other reasons, e.g. emacs -nw. I could try to write a script to parse the command line arguments myself, but that sounds tedious and error prone, so I'm hoping to avoid that if possible.

1

Yes, you can use variable command-line-args to obtain the list of command-line arguments passed to Emacs. That will tell you whether a file name was passed as argument.

C-h v command-line-args tells you something like this:

command-line-args is a variable defined in C source code. Its value is

("C:\\my-emacs\\bin\\emacs.exe" "c:\\some-file-or-directory-name")

Documentation:

Args passed by shell to Emacs, as a list of strings.

Many arguments are deleted from the list as they are processed.

  • Wouldn't command-line-args include all tokens passed to the emacs executable, including other flags and such? – Kwarrtz Jun 16 '18 at 7:49
  • Yes. You would filter that list to remove args other names of files to visit (not load). – Drew Jun 16 '18 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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