I am looking for a way to access the value of %HOMEPATH% under Windows (or more generally, any environment variable of the form %name%).

The use case is to have a consistent init.el across multiple operating systems. In Linux and OS X, (expand-file-name "~") always gives the home folder, but the same command under Windows it returns %HOMEPATH%/AppData/Roaming.

  • 1
    Not on Windows right now, but I'd presume that (getenv "HOMEPATH") would work.
    – legoscia
    Aug 19, 2015 at 13:47
  • (expand-file-name "~") gives what Emacs considers to be the home folder, see gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html
    – npostavs
    Aug 19, 2015 at 14:39
  • @npostavs Right, it's what what emacs treats as the home folder. Unfortunately, the location %HOMEPATH%/AppData/Roaming is quite inconvenient since most other things (e.g. Documents, Dropbox, etc) are stored in %HOMEPATH%.
    – Kevin
    Aug 19, 2015 at 14:41
  • As explained in the manual, you can set HOME to change what Emacs considers to be the home folder.
    – npostavs
    Aug 19, 2015 at 15:11

1 Answer 1


I found the answer to my own question; the relevant command is

(substitute-in-file-name "$HOMEPATH")

More information can be found in the official documentation here.

(The solution provided by @legoscia above also works.)

  • 1
    The answer for the general question of how to access environment variables of the form %name% is to use getenv, as @legoscia suggested. The substitute-in-file-name function solves the more narrow problem of resolving variables in a file name. (Note that it also has special handling for paths that contain '//' or '/~'. I haven't checked but that /might/ be problematic on Windows if you happen to have a double slash in a path or variable? Windows ignores the double slash.)
    – glucas
    Aug 19, 2015 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.