I want to synchronize my emacs configuration through an identical init.el but needs some settings defined according to the host.

For instance although init.el will be the same custom-file will vary by account and host, but will still have to be in the same version control repo so I want something like this for the custom-file, this example using shell syntax

(setq custom-file "~/.emacs.d/$USER_$HOST_custom.el")
    (file-exists-p custom-file)
    (load custom-file)

What would be the idiomatic elisp for such a construct?

(substitute-in-file-name "~/.emacs.d/${USER}_${HOST}_custom.el")

Should give you what you want.

substitute-in-file-name is a built-in function in C source code.

 (substitute-in-file-name FILENAME)

Substitute environment variables referred to in FILENAME. $FOO where FOO is an environment variable name means to substitute the value of that variable. The variable name should be terminated with a character not a letter, digit or underscore; otherwise, enclose the entire variable name in braces.

If /~ appears, all of FILENAME through that / is discarded. If // appears, everything up to and including the first of those / is discarded.

  • For the record, I disagree with the edit. But you already spoiled it, so it's no point to undo the edit. – wvxvw Jul 31 '17 at 14:02
  • Is the $FOO system applicable anywhere in emacs or are there just a few areas like filename processing where it is applicable? – vfclists Jul 31 '17 at 23:04
  • @vfclists I'm not sure I understood your question, but if you are asking about availability of certain variables in Emacs, then Emacs will inherit environment variables from the login shell (I believe), meaning, it doesn't process user's shell configuration files. – wvxvw Aug 1 '17 at 6:53
  • I have a similar question as @vfclists. Can we use $FOO or ${FOO} for any other commands in init.el ? i.e. other commands other than substitute-in-file-name. – user1783732 Apr 11 '20 at 17:02

Try this:

(setq custom-file (format "~/.emacs.d/%s_%s_custom.el"
                          user-login-name (getenv "HOST")))
(when (file-exists-p custom-file)
    (load custom-file))
  • I take it that (getenv "USER") could be a substituted for user-login-name? – vfclists Jul 31 '17 at 23:02
  • Yes, that's correct – justbur Aug 1 '17 at 12:06

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.