Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Emacs on Windows XP and whenever I type in "~/" it automatically replaces that with a directory, in my case its "C:/Documents and Settings//Application Data". Is there any way I can get that to point to another directory like, say, my projects directory on the Desktop? Every time I need to make a new file there I have to C-x C-f + the full path, and although tab completion makes it faster than it'd normally be, it'd be nice to have that one directory bound to something like "~/" for easy access. Is there any way that could be done?

share|improve this question
up vote 5 down vote accepted

All you need to do is to set the %HOME% environmental variable.

This can be done by going to:
start->settings->control panel->system
Click on the advanced tab. At the bottom is a button: "Environmental Variables".

Create a new variable called HOME and put the path you want in there.

Since most windows programs don't care about this variable, it is unlikely to affect anything else for you.

This can also be done by editing the registry entries for emacs. Here is some information on the registry variables: Ms Windows Registry.

share|improve this answer
It is, of course, also possible to set HOME in a batch file if that's easier (for example, if you're setting up a "portable" emacs on your flash drive). – SamB Nov 18 '10 at 20:51

The ~ seems to be handled by expand-file-name, and it does not mention any way to change how it is expanded. But it does use the HOME environment variable. You could specify a new value for HOME, but that might confuse other things that use HOME as a place to store files (customize, etc. that might want to read from or write to files under HOME).

As an experiment, I wrote up some advice to find-file that temporarily changes the value of HOME. The idea is to minimize the amount of time where the alternate value is active.

(defvar fake-HOME-value "/var/tmp"
  "An alternate value to use for the HOME environment variable
while the 'fake-HOME' around advice is active.")

(defadvice find-file (around fake-HOME)
  "Temporarily replace the HOME environment variable with
the value from 'fake-HOME-value'."
  (let ((original-HOME-value (getenv "HOME")))
          (setenv "HOME" fake-HOME-value)
      (setenv "HOME" original-HOME-value))))

(defun disable-fake-HOME ()
  "Disable the fake-HOME advice for find-file."
    (ad-disable-advice 'find-file 'around 'fake-HOME)
    (ad-update 'find-file)))

(defun enable-fake-HOME ()
  "Enable the fake-HOME advice for find-file."
    (ad-enable-advice 'find-file 'around 'fake-HOME)
    (if (ad-is-active 'find-file)
        (ad-update 'find-file)
      (ad-activate 'find-file))))


Put it in your .emacs (or an .el file, and (load-file "/path/to/your.el")). I can not test it on Windows, but the only thing that is platform specific is the use of HOME, but I think Emacs converts the Windows equivalent (a combination of several environment variables?) to HOME.

share|improve this answer
You generally want those things to read/write files under the same directory as ~ gets you to anyways, so that's not really a problem... – SamB Nov 18 '10 at 20:49

~ is a very old and well established synonym for 'the user's home directory' and that logic is probably buried deep inside emacs and unlikely to change unless you're willing to rebuild it from source after finding the appropriate code and changing it. On the other hand, you could probably set your shortcut to start emacs to start in your chosen directory, allowing you to save and load files without specifying a path. Probably.

share|improve this answer
Love it or hate it, there is one thing that you can say about GNU Emacs; there is very little that you can not change without rebuilding from source. – Richard Hoskins Apr 18 '10 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .