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


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.

# Extra dirs to add to $PATH

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 ()
    (insert-file-contents "~/.mylocal-settings")
    (dolist (line (split-string
                   (substitute-env-vars (buffer-string))
       ((not (string-match "\\`\\(?:#.*\\|[ \t]*\\|\\([^ ]+\\)=\\(.*\\)"
        (message "Can't parse line: %S" line))
       ((not (match-beginning 1)) nil) ;; Comment or empty line.
        (setenv (match-string 1 line) (match-string 2 lines)))))))


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

| improve this answer | |

The answer you refered to does not apply to newer versions of macOS - try some of the other answers using launchctl.

Alternatively look at the emacs package github.com/purcell/exec-path-from-shell which allows you to read environment variables, not just your PATH, from a copy of your startup scripts

| improve this answer | |

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.