Emacs: Environment Variables in Emacs

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




Set Environment Variable within Emacs

You can set environment variables within emacs.

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

Here's the elisp code to set environment variable within emacs only:

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

[see Evaluate Emacs Lisp Code]

Emacs exec-path

Emacs has a variable named exec-path. Its value is a list of dir paths. Emacs uses exec-path to find executable binary programs.

For example,

If emacs complains that it cannot find these programs, the problem is probably with your exec-path.

By default, emacs copies the value of (getenv "PATH") to exec-path. So, their values should be identical.

Here's a example of setting exec-path:

(when (string-equal system-type "windows-nt")
  (setq exec-path
          "C:/Program Files (x86)/Emacs/emacs/bin/"
          "C:/Program Files (x86)/Emacs/EmacsW32/gnuwin32/bin/"

[see Emacs Lisp: Determine OS, Emacs Version, Machine Host Name]

Difference between exec-path and PATH

The value of (getenv "PATH") and exec-path do not need to be the same.

Emacs Lisp Code for Setting PATH and exec-path

Here's emacs lisp code template to set both PATH and exec-path in sync.

(when (string-equal system-type "windows-nt")
  (let ((xPaths
           "C:/Program Files (x86)/nodejs/"

    (setenv "PATH" (mapconcat 'identity xPaths ";"))

    (setq exec-path (append xPaths (list "." exec-directory)))))


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

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Init

Init Basics



Text Editing



Restore Things





Advanced Init Tips