Is there a function/variable to access the directory the init file is in?

This could be: ~/.config/emacs/ or ~/.emacs.d/, for e.g.

  • 2
    Your question is not clear: what do you mean by "access"? Are you looking for something like (find-file (file-name-directory user-init-file))?
    – Basil
    Aug 31 '19 at 10:17
  • Any advantage to this over user-emacs-directory ?
    – ideasman42
    Aug 31 '19 at 12:04
  • No, if user-emacs-directory works for you, then it's best to use that. The only difference is that user-emacs-directory is a defconst, whereas user-init-file is a defvar, but in most cases this should not matter.
    – Basil
    Aug 31 '19 at 13:30

If you want to access the directory from within the init.el file, this bit of code will give you the name of directory that init.el is in while it is being executed.

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

If you want to access that directory in another file, then just save that in a variable.

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.