5

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.

liwp
  • 6,746
  • 1
  • 27
  • 39
roshanvid
  • 793
  • 6
  • 21

6 Answers6

14

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.

  • 2
    FWIW, since I wrote this answer there is now a [package in MELPA](http://melpa.org/#/exec-path-from-shell) which solves this problem, named `exec-path-from-shell`. It would probably be best to install that package rather than copying and pasting this snippet. As well as being properly maintained, it also allows copying other environment variables from your normal shell setup, so any subprocesses started from Emacs will work as expected. –  Oct 24 '14 at 22:56
4

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.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • 1
    This will definitely work, but isn't there a way to change the login shell on Mac OS? That would be the definite solution. – pmr Feb 24 '12 at 19:32
  • Im interested in that as well, I think that's a question well suited to superuser – Arthur Ulfeldt Feb 24 '12 at 19:33
  • @pmr, @ArthurUlfeldt The login shell can be changed with the help of the `chsh` command (as in many other Un*ces). I'm not sure whether that affects Emacs, though. – Matthias Benkard Feb 25 '12 at 13:20
  • 1
    You can also just start Emacs on OS X normally and use an elisp library called [exec-path-from-shell](https://github.com/purcell/exec-path-from-shell) to set Emacs' $PATH to match that seen in the shell. Installable packages are available in Marmalade and [Melpa](http://melpa.milkbox.net/). – sanityinc Sep 13 '12 at 09:50
4

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 ;-(

Stefan
  • 201
  • 1
  • 2
  • Although not really an answer, the anecdotal value of this is great. – pmr Feb 25 '12 at 14:06
  • It was mostly fixed in gdm2 which would source .profile and .xprofile but from what I hear it got broken again in gdm3. Kdm seems to do some crazy stuff when zsh is detected and starts running everything through zsh without using compatibility mode. – Arrowmaster Feb 25 '12 at 20:43
2

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>
sw1nn
  • 7,278
  • 1
  • 26
  • 36
1

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.

pmr
  • 58,701
  • 10
  • 113
  • 156
0

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.

ctpenrose
  • 1,467
  • 2
  • 18
  • 28