Given you started Emacs passing a LISP file through command line:

emacs --load .custom.d/init.el

how it could be possible to dynamically obtain custom.el directory location?

Since the default init.el is not used, buffer-file-name is not a possible solution:

(file-name-directory (or load-file-name (buffer-file-name)))

I have also tried to eval default-directory as follows:

cd /tmp/username
mkdir custom.d
touch custom.d/init.el
emacs -nw --load custom.d/init.el --eval "(print default-directory)"

however the directory from where emacs was invoked (i.e. /tmp/username in) is shown, not the init.el folder.


Emacs manual node Action Arguments tells you this about command-line option --load:

Load a Lisp library named FILE with the function load. If FILE is not an absolute file name, Emacs first looks for it in the current directory, then in the directories listed in load-path (*note Lisp Libraries::).

Warning: If previous command-line arguments have visited files, the current directory is the directory of the last file visited.

This tells you that at the time that command-line switch is interpreted, the value of default-directory (the "current directory") is the directory checked first. And that starts out as the directory in which you launched Emacs, which is likely the current working directory in which you issued the command to launch it. See also variable invocation-directory.

To get the value dynamically, just evaluate variable default-directory at the beginning of your custom-file.

Or if you need to get the value outside of Emacs, and if loading your custom-file does not change the default-directory then you can use switch -f just after that --load custom-file, to invoke a function that prints the value of default-directory.

  • I've tried to eval both variables as suggested: default-directory correspond to the directory from where emacs was called; exec-directory is void. What I actually I trying to do is set spacemacs-start-directory from inside a custom init.el based on its own location.
    – JP Ventura
    Mar 30 '20 at 21:46
  • I meant invocation-directory, not exec-directory; corrected now.
    – Drew
    Mar 30 '20 at 21:55
  • I don't know what you mean by "its" in "based on its own location". (Doesn't spacemacs-start-directory get set automatically by Spacemacs? I don't use Spacemacs, so I can't help in that regard.)
    – Drew
    Mar 30 '20 at 21:57
  • @JPVentura --load isn't processed until after the init file has been processed. I don't use Spacemacs, but I'm going to guess that this is a problem for your plan, if your goal is to (more or less) use a different init file.
    – phils
    Mar 31 '20 at 1:52
  • Although emacs-startup-hook runs after all of that, so if Spacemacs waits for that before doing anything with spacemacs-start-directory then it's probably ok.
    – phils
    Mar 31 '20 at 1:58

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.