Emacs: Environment Variables
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
- If emacs is started from a text terminal, it inherits shell's environment variables.
- If emacs is started from clicking icon in GUI, it does not inherit environment variables from your shell, but does inherit the environment variables from
~/.MacOSX/environment.plist
.
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")))
- You can use slash
/
for dir separator, works in Microsoft Windows too. - Dir path may end with a path separator
/
, or without. - The builtin variable path-separator can be used instead of
;
.
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: Microsoft Windows FAQ
- Emacs: Microsoft Windows: Init File Location
- Emacs: Microsoft Windows: Start Emacs at Home Dir
- Emacs: Microsoft Windows: Start Emacs from Terminal
- Emacs: Microsoft Windows: Install Linux Tools
- Emacs: Microsoft Windows: Run PowerShell
- Emacs Init: Change Default Shell (PowerShell)
- Emacs: Environment Variables