env-var-import

env-var-import

Writing my last post on setting environment variables in GUI Emacs got me thinking about a more general solution to that problem. Rather than requiring everyone to define a function like set-exec-path-from-shell-PATH, it would be nice if there were a package that would handle importing everything for you given a list of environment variables.

To that end I wrote env-var-import. It’s available in Marmalade for easy installation: package-install env-var-import.

Once you’ve installed it, add (require 'env-var-import) to your Emacs configuration and you’re ready to do some importing!

You have two options for using it:

  1. (env-var-import): In this mode, it will only import the environment variable defined by env-var-import-exec-path-var and set exec-path to the value of that variable. env-var-import-exec-path-var defaults to to PATH but can be customized.
  2. (env-var-import '("VAR1" "VAR2")): In this mode it will still import the value of the environment variable defined in env-var-import-exec-path-var and set exec-path to that value. However, it will also import the values of VAR1 and VAR2.

Setting Environment Variables in GUI Emacs

I use Emacs for most of my development work. When I’m not SSHed into a remote machine I generally stick with the GUI Emacs. On OS X this has the downside of not inheriting environment variables from your shell.

I could work around this by using setenv, but that would require me to duplicate the values of those variables. Thankfully there is a better solution:

This will invoke the shell to get the value of $PATH and then use setenv to set it. Then all you need to do is add (if window-system (set-exec-path-from-shell-PATH)) to your Emacs configuration.

There is nothing special about $PATH in this regard. You could easily extend this function to set other environment variables as needed.

I originally found this code on the Clojure mailing list.

Winds of Change

When I started this blog in early 2013, I intended it to be primarily about technology. I never wrote anything in that vein, so several months later I started posting short horror stories.

I plan to keep writing fiction, but I would like to have a normal tech blog as well. As such, I’ve moved all my fiction to Library of Shadows. All new stories will be posted there.