22

I am using a Windows 7 computer and do not have administrator access. Therefore I can not place a dot emacs file in the root directory. Is there a way for me to specify a different location for the dot emacs file?

1
  • If you can control your own location of the Emacs installation (e.g., putting it wherever you want, or at least have write access to the installation), then you can take complete control by using something like site-start.el to load your own init.el and you no longer need a .emacs. In this example, you don't need to fiddle with the home directory (unless someone else on the computer is putting stuff in there that will affect what you do). In other words, it can be blank and you never need to touch it.
    – lawlist
    Commented Jun 3, 2015 at 19:14

8 Answers 8

7

Emacs has several places it looks for init files, as documented in the manual. If you want to customize Emacs you must start with one of those files.

A common approach is to use the file init.el in your user-emacs-directory, instead of having a .emacs in your HOME directory.

The user-emacs-directory defaults to ~/.emacs.d, so it is still relative to your HOME, but the benefit of this approach is you can keep everything related to your Emacs configuration in one place. That directory could be under version control, backed up, shared across multiple machines, etc. You can use a symlink (mklink on Windows) to keep the files in some other location.

For example, it is quite common to see folks maintain their Emacs configuration on Github. You can find lots of examples of this by searching Github for elisp projects with init.el files.

3
  • 1
    I like this solution and would like to use it however it still does not tell me how to change my user-emacs-directory
    – Startec
    Commented Jun 4, 2015 at 0:11
  • My suggestion was to use symlinks, really. As shown by the other answers you need to put something in one of the standard locations Emacs reads during init, even if all it does is load your config from somewhere else.
    – glucas
    Commented Jun 4, 2015 at 0:22
  • The manual link is broken.
    – corvus_192
    Commented Sep 10, 2017 at 10:20
31

On every Windows install on which I use Emacs, I have the following code in a .emacs file at the listed location:

;; Place this file in C:\Users\Username\AppData\Roaming and point to the appropriate files
(setq user-init-file "C:/path/to/.emacs")
(setq user-emacs-directory "C:/path/to/.emacs.d/")
(setq default-directory "C:/whatever/you/want/to/start/in")
(setenv "HOME" "D:/my/home/directory")
(load user-init-file)

This way I can put my .emacs and .emacs.d/ anywhere I want.

4
  • Thank you, almost just what I needed. I left off the default-directory and sentenv HOME but otherwise it's what I needed to setq! Commented Dec 10, 2017 at 4:01
  • I tried Ryan's answer. But, now I am not able to open this file within emacs from AppData\Roaming folder. Kindly excuse me, as I couldn't add comment because, I don't have 50 points yet. Commented Dec 16, 2017 at 9:53
  • @SreekumarR Your should ask a new question, linking to this one, instead of posting an "Answer" Commented Dec 16, 2017 at 11:14
  • @SreekumarR If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Stefan
    Commented Dec 16, 2017 at 13:18
5

Your .emacs file goes in your HOME directory. From the Emacs manual:

The Windows equivalent of HOME is the user-specific application data directory. The actual location depends on the Windows version...

You can override this default value of HOME by explicitly setting the environment variable HOME to point to any directory on your system. HOME can be set either from the command shell prompt or from ‘Properties’ dialog of ‘My Computer’. HOME can also be set in the system registry, see MS-Windows Registry.

4
  • 2
    Yes, I know how to set the home directory however for version control reasons I want to be able to set emacs to read from a different directory (so that I can have a directory specific to emacs)
    – Startec
    Commented Jun 3, 2015 at 18:21
  • 3
    Note that this hack makes Emacs consider something else as the home directory.
    – wasamasa
    Commented Jun 3, 2015 at 18:34
  • @wasamasa - I wouldn't call it a "hack". On my build of Emacs 25, if you don't set "HOME" with an environment variable, it defaults to the Emacs install directory, which is quite clearly wrong. "HOME" should be set to what the user considers their home directory, and setting the environment variable is the intended standard way to do that. On Unix systems this is usually already done for the user during the login process, but on Windows you have to manually do it.
    – T.E.D.
    Commented Sep 19, 2019 at 20:40
  • The point of this is to change what you'd normally consider your HOME directory to something wildly different. Hence why I call that change a hack. Setting it to its appropriate value is obviously fine.
    – wasamasa
    Commented Sep 20, 2019 at 6:11
2

You say:

I know how to set the home directory however for version control reasons I want to be able to set emacs to read from a different directory (so that I can have a directory specific to emacs).

Your init file goes in your home directory, which you can put anywhere. Apparently you want to have a different directory for stuff that Emacs loads. That's not a problem. Put your init file in your home directory, and have that file add another directory to the value of variable load-path and then require whatever other Emacs libraries you want to use. Put those libraries in that other directory.

IOW, your init file need not contain anything beyond setup of the directory where you keep the Emacs libraries that you want to load.

1

Just for the sake of completeness, I provide a link to:

https://www.gnu.org/software/emacs/manual/html_node/efaq-w32/Location-of-init-file.html

Which says that you can set a HOME value in the registry path:

HKCU\SOFTWARE\GNU\Emacs\HOME

But this value is overridden by the environment variable HOME.

1

Just define HOME in Windows Environment Variables. Recent versions of Emacs for windows will assume that automatically.

0

Think you could put (setq user-init-file "C:/path/to/.emacs") and other lines proposed by @ryan into site-start.el

Your site may also have a site startup file; this is named site-start.el, if it exists. Like default.el, Emacs finds this file via the standard search path for Lisp libraries. Emacs loads this library before it loads your init file.

0

A lot of answers mention that you can change the default emacs location by adding/changing the %HOME% environment variables in Windows. It is also recommended in the emacs documentation, but

Warning

But it's important to note that changing this environment variable can cause un-intended impact on your system. I'm trying to list the issues that I have faced below
(Right now, I have only identified 1 issue, but I'm hoping to get more inputs from the community for future users):

  1. Git may not work as expected - since Git also uses %HOME% in int process

Solution

So the ideal approach to solve this issue is what Ryan has suggested to use setenv because

Your shell in which you've started the Emacs hands over a copy of its environment to its child process (the Emacs) ... Any change the Emacs then performs with its inherited environment will only affect the Emacs process's environment. There is no way the Emacs's environment can influence the environment of the shell.

Reference: https://stackoverflow.com/a/17450820/6908282

(setenv "HOME" "D:/my/home/directory")

An additional useful information to note is that, in Windows the HOME environment variable is not set by default (reference1 & reference2).

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.