Emacs: Environment Variables

By Xah Lee. Date: . Last updated: .

This page shows you how to set environment variables in emacs, especially if you have problems in Windows emacs of getting linux commands to run.

Where Emacs Gets Environment Variable

Windows

Emacs inherit environment variables, regardless starting from terminal or clicking icon in GUI.

(perm env var is stored in the Registry. 〔see Windows Environment Variable Tutorial〕).

Mac

Linux

If emacs is started from a text terminal, it inherits shell's environment variables.

On linux, you should not start emacs from clicking icon created by the linux desktop. If you want to click icon to launch emacs, create a bash script that launch emacs.

That way, it'll inherit environment variable.

Or, best, setup a key to launch emacs.

Get Environment Variable Within Emacs

;; get value of env var PATH
(getenv "PATH")

〔see Evaluate Emacs Lisp Code

Set Environment Variable within Emacs

You can set environment variables within emacs.

This lets emacs to have environment variables independent of the operating system.

;; set env var PATH, by appending a new path to existing PATH value
(setenv "PATH"
        (concat
         "C:/cygwin/usr/local/bin" path-separator
         "C:/cygwin/usr/bin" path-separator
         "C:/cygwin/bin" path-separator
         (getenv "PATH")))

exec-path

Reference

2012-07-31 Thanks to Steve Purcell ❮https://twitter.com/sanityinc❯ for path-separator.

Emacs Lisp, Variable

Emacs on Microsoft Windows

Emacs Lisp, Call Shell Command

Emacs Lisp, Get Version, OS Type, System Info, etc