Emacs: Set Environment Variables within 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 aspell or other unix utils to run.

Setting Environment Variable within Emacs

You can also set environment variables within emacs without setting them in the OS.

You might want to do this if you are using Cywin. You want cygwin's bin paths in emacs, but you don't necessarily want them when running cmd.exe in Windows console, because you might want that to stay pure Windows.

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

;; example of setting env var named “path”, by appending a new path to existing path
(setenv "PATH"
   "C:/cygwin/usr/local/bin" ";"
   "C:/cygwin/usr/bin" ";"
   "C:/cygwin/bin" ";"
   (getenv "PATH")
;; show env var named path
(getenv "PATH")

(to evaluate elisp code, select it and Alt+x eval-region. [see Evaluate Emacs Lisp Code])

Emacs's 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, when spell checking, emacs will try to find ispell or aspell in exec-path. When you press Z to compress file in dired, emacs will try to find gzip or gunzip in exec-path. When you call any of emacs commands: {diff, grep, shell}, emacs will try to find the program in exec-path too.

If emacs complains that it cannot find ispell, aspell, ftp, gzip, etc, 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.

Difference between exec-path and PATH

If you did set the PATH env var within emacs, you probably also want to adjust your exec-path. 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/"

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

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

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 (
           ;; "C:/Python32"

           "C:/Program Files (x86)/nodejs/"


           "C:/Program Files (x86)/ErgoEmacs/msys/bin"
           "C:/Program Files (x86)/Mozilla Firefox/"
           "C:/Program Files (x86)/Opera"
           "C:/Program Files (x86)/Safari"
           ) )

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

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


Thanks to Steve Purcell https://twitter.com/sanityinc for path-separator.

Emacs Customization

  1. Emacs init file
  2. Install Packages
  3. Install Package Manually
  4. Define Keys
  5. M-x customize
  6. What's Major Mode?
  7. What's Minor Mode?
  8. Set File to Open in a Major Mode
  9. Organize Init File
  10. Byte Compile Elisp
  11. What's Hook?
  12. Environment Variables in Emacs
  13. Set Default Window Size
  14. Font Setup
  15. Set Color Theme
  16. Turn Off Auto Backup
  17. Check OS, Version, Host Name
  18. Check Defined/Loaded

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp