0

I'd like to set things up so that I can use emacs on multiple computers, with the .emacs.d directory shared amongst them (via DropBox).

My solution so far has been to use environment variables which are then read in my init.el. The computer-specific environment variable then tells me where to find the computer-specific recentf file, etc, etc. On Windows this works great because one can set environment variables via a control panel option.

I'm trying to figure out how to get computer-specific environment variables to work in emacs on MacOS. Ideally, I'd like to be able to launch emacs from both Spotlight and from a terminal window (iTerm2, specifically).

I found a StackOverflow answer about getting environment variables into GUI apps but it doesn't seem to work (emacs still has troubles loading the computer-specific files because the environment variable doesn't appear to be set in the init.el).

I'd like to avoid setting up .bash_profile to set the environment variables so that I can share my .bash config files through DropBox, as well

MikeTheTall
  • 659
  • 4
  • 11
  • Does this answer your question? [How to make terminal/shell spawned inside Emacs running on a Mac inherit the environment variables of the native terminal?](https://emacs.stackexchange.com/questions/17249/how-to-make-terminal-shell-spawned-inside-emacs-running-on-a-mac-inherit-the-env) – mmmmmm Oct 14 '20 at 08:52

1 Answers1

1

Maybe in your case the best option si to cook up your own solution. E.g. set your machine-specific env-vars in your own invented file, say ~/.mylocal-settings.sh, where you could use a subset of sh syntax, like:

# Place where I keep my foos.
FOOVAR=/foo/bar

# Extra dirs to add to $PATH
MYEXTRAPATH=$HOME/myextra/bin:/myopt/bin
...

then in your ~/.bashrc or ~/.bash_profile you can just do:

source ~/.mylocal-settings.sh

and then in your ~/.emacs you could do:

(defun my-read-local-settings ()
  (with-temp-buffer
    (insert-file-contents "~/.mylocal-settings")
    (dolist (line (split-string
                   (substitute-env-vars (buffer-string))
                   "\n"))
      (cond
       ((not (string-match "\\`\\(?:#.*\\|[ \t]*\\|\\([^ ]+\\)=\\(.*\\)"
                             line))
        (message "Can't parse line: %S" line))
       ((not (match-beginning 1)) nil) ;; Comment or empty line.
       (t
        (setenv (match-string 1 line) (match-string 2 lines)))))))

(my-read-local-settings)

This way you can keep ~/.bashrc and friends identical between hosts as well.

Stefan
  • 26,154
  • 3
  • 46
  • 84