When I launch the emacs GUI, PATH is not set depending on my .zshrc. How do I source my .zshrc to set it correctly?
Edit: I'm trying to use swank-clojure but it can't find lein since it's not on the PATH
.
When I launch the emacs GUI, PATH is not set depending on my .zshrc. How do I source my .zshrc to set it correctly?
Edit: I'm trying to use swank-clojure but it can't find lein since it's not on the PATH
.
Edited to add (2014): There is now a package in MELPA named exec-path-from-shell
, which solves this exact problem. (See also the Github repository). It's almost certainly better to install that package rather than copying and pasting this snippet. In addition to being properly maintained, it also supports copying other environment variables from your shell setup, so any subprocesses started from Emacs will work as expected.
Original answer: Here's my workaround for this problem, which doesn't require always starting Emacs from a shell (I am also bothered by this sometimes on my mac):
(let ((path (shell-command-to-string ". ~/.zshrc; echo -n $PATH")))
(setenv "PATH" path)
(setq exec-path
(append
(split-string-and-unquote path ":")
exec-path)))
It sets both Emacs' idea of the PATH
environment variable (so it will be inherited by subshells) and the Lisp variable exec-path
, which is used for finding executables from Lisp code and for some autocompletion purposes. You might also need to check that shell-file-name
is customized to the location of your zsh
binary instead of the default /bin/bash
.
This answer is for people who find this question later and are having this problem because of Emacs default behaviour on the Mac,
If you are on a Mac and wondering how to get emacs to start from the shell where it will pickup your customized ENV in graphical mode instead of text only then start it from zsh with something like this:
/Applications/Emacs.app/Contents/MacOS/Emacs
This should cause emacs to see all your shell (zsh or otherwise) customizations.
FWIW, this problem is not specific to Mac OS X, since it also affects GNU/Linux systems, where starting Emacs from the GUI's menu-bar will typically result in an Emacs that does not have the same PATH as one launched from the command-line. This problem dates back to the first `xdm' Xsession scripts, and while they are fairly easy to fix (basically use an Xsessionwrapper script that does "exec $SHELL -c Xsession" so the shell gets run before running the user's Xsession), here we are 20 years later suffering from the same old problem. And somehow I don't expect gdm3 to fix this ;-(
Mac OS X has a special environment file to allow setting enviroment variables for process launched by a specific user. This allows you to set environment variables for GUI apps. In your case you can use this to set the PATH for any emacs launched by clicking on an icon in dock or finder.
This file lives at
~/.MacOSX/environment.plist
You can create the file in XCode, but it's just plain xml so the contents of the file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PATH</key>
<string>~/bin:/usr/local/bin:/usr/local/sbin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:</string>
</dict>
</plist>
The variable Emacs uses instead of PATH
is called exec-path
(C-h v exec-path RET
for the documentation and state). exec-path
is initialized from PATH
or EMACSPATH
. It seems you are not launching Emacs from your zsh
so your Emacs path does not exactly match the one in your shell.
You might try setting exec-path
it in your .emacs
or init.el
although you will have to maintain both. I don't see any way to source your shell configuration directly, but maybe you could just regexp your path out of it.
You can also specify your PATH variable within a .zshenv
file -- the preferred convention for assigning environment for sub-shells. As long as zsh is configured as your user's default shell, emacs will source your .zshenv
regardless of how emacs is invoked.