1

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")
(if
    (file-exists-p custom-file)
    (load custom-file)
  )

What would be the idiomatic elisp for such a construct?

3
(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
2

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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