5

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?

vfclists
  • 1,347
  • 1
  • 11
  • 28

2 Answers2

6
(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.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
  • 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 01 '17 at 06: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
3

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))
justbur
  • 1,500
  • 8
  • 8