127

I tried looking for the .emacs file for my Windows installation for Emacs, but I could not find it. Does it have the same filename under Windows as in Unix?

Do I have to create it myself? If so, under what specific directory does it go?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ray
  • 187,153
  • 97
  • 222
  • 204

16 Answers16

119

Copy and pasted from the Emacs FAQ, http://www.gnu.org/software/emacs/windows/:

Where do I put my init file?

On Windows, the .emacs file may be called _emacs for backward compatibility with DOS and FAT filesystems where filenames could not start with a dot. Some users prefer to continue using such a name, because Windows Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it. In Emacs 22 and later, the init file may also be called .emacs.d/init.el. Many of the other files that are created by Lisp packages are now stored in the .emacs.d directory too, so this keeps all your Emacs related files in one place.

All the files mentioned above should go in your HOME directory. The HOME directory is determined by following the steps below:

  1. If the environment variable HOME is set, use the directory it indicates.
  2. If the registry entry HKCU\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates.
  3. If the registry entry HKLM\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates. Not recommended, as it results in users sharing the same HOME directory.
  4. If C:\.emacs exists, then use C:/. This is for backward compatibility, as previous versions defaulted to C:/ if HOME was not set.
  5. Use the user's AppData directory, usually a directory called Application Data under the user's profile directory, the location of which varies according to Windows version and whether the computer is part of a domain.

Within Emacs, ~ at the beginning of a file name is expanded to your HOME directory, so you can always find your .emacs file with C-x C-f ~/.emacs.

There's further information at HOME and Startup Directories on MS-Windows.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Node
  • 21,706
  • 2
  • 31
  • 35
  • 1
    Or you may need to find with C-x C-f ~/_emacs (instead of the period). Using the underscore for your init is common (and suggested) practice for both Vim and Emacs. – labyrinth Jan 26 '12 at 23:41
  • Emacs complains that "_emacs" is deprecated :( – Brian Z Feb 05 '15 at 12:59
  • Thanks. This explanation actually helped me set up emacs properly following a clojure tutorial, where they didnt even mention this. Created the HOME variable on windows and now it works. – Siniyas Sep 15 '15 at 10:43
  • For me in reference to your point 5 above, the directory emacs is looking in for me is C:\Users\userid\AppData\Roaming on Windows 7 but of course my computer is on a domain. – user8128167 Nov 22 '16 at 16:24
  • On Windows 10 I set the variable HOME to be %USERPROFILE% to avoid the AppData suffix. – Bae Jan 18 '17 at 03:17
97

It should be stored in the variable user-init-file. Use C-H v user-init-file RET to check. You can also open it directly by using M-x eval-expression RET (find-file user-init-file) RET

cruizh
  • 2,839
  • 3
  • 18
  • 20
gone
  • 4,342
  • 2
  • 24
  • 29
30

Open the file like this in Emacs for Windows:

C-x C-f ~/.emacs

More information in the Emacs Wiki

sverrejoh
  • 16,464
  • 13
  • 41
  • 29
  • 11
    And then click File > Open File... or Save As... to see where ~/ is in a familiar Windows Open dialog. – Yoo Feb 02 '11 at 03:16
23

On my Vista box it's in C:\Users\<USER>\AppData\Roaming\

Alexander Kojevnikov
  • 17,580
  • 5
  • 49
  • 46
  • 1
    Same for windows 8.1 (Emacs 24) with an exception. When Emacs is opened from Windows Explorer, a link or cmd console it uses C:\Users\\AppData\Roaming\.emacs profile file but if you open Emacs from PowerShell console, Emacs will look for .emacs file in C:\Users\ folder. – kri Jul 03 '15 at 14:38
  • This worked for me. I created a .emacs file in the Roaming directory in AppData, and works like a charm! Windows 10. – Retro Gamer Jul 16 '18 at 21:25
  • Worked in Windows 10 – Alex Kaszynski Jun 20 '19 at 14:26
7

Note that it may NOT be enough to just type Ctrl-x Ctrl-f ~/.emacs and create the file.

It may be that your Emacs application uses a different place to store your init file, and if so, then creating the file ~/.emacs simply creates a useless file which your Emacs application ignores.

Also, you may want to do more than just access the .emacs init file, but you may want to know where it is, i.e., its pathname.

To get at this there are two methods:

Easy way: type Ctrl + H V user-init-file Return

Slightly trickier way: You can find out where your system is storing its own .emacs file by:

  1. Click options and scroll down to "Set Default Font..."
  2. Change the font setting and click okay
  3. On the options menu, go down to "Save Options"
  4. When the options are saved, the system saves its .emacs file, and you can read the file path in the minibuffer at the bottom of the Emacs screen
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3020269
  • 71
  • 1
  • 1
  • Please refer the docs about formatting your answer. Its quite easy and will earn you more reputation. – Bhavik Shah Nov 22 '13 at 04:32
  • When I saved a file named ~/.emacs.el file, it was created in ```c:/Users/user1/AppData/Roaming/.emacs.el```. This site https://www.emacswiki.org/emacs/DotEmacsDotD helped me realize I needed to name the file ```~/.emacs.d/init.el``` which meant the file in the file system was at ```c:\Users\user1\AppData\Roaming\.emacs.d\init.el``` (not at all what I expected). I prefer setting the HOME environment variable and place the file in my ```%USERPROFILE%``` directory which is the logical HOME directory on windows (at least as far as I'm concerned). – PatS Nov 02 '18 at 22:05
4

On versions of Emacs on Windows above 22, it seems to have moved to

~/.emacs.d/init.el

, ~ being the value of your environment variable HOME (see Control Panel → SystemAdvancedEnvironment variables).

The file itself might not exist. In that case just create it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gauthier
  • 40,309
  • 11
  • 63
  • 97
4

In Windows 7 put your init.el file in C:\Users\user-name\AppData\Roaming\.emacs.d\, where user-name is your user/login folder.

Take care so your init.el file won't be named init.el.txt. This is something Windows does if you create your file with some editor like Notepad.

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
caisah
  • 1,959
  • 1
  • 21
  • 31
4

You must create an emacs initialization file. One is not automatically created. I had a similar issue and this answer tracks down what I did.

My issue was my ~/.emacs.el file was not loading. Strange because this has always worked for me.

This question/answer helped me but I had to put my init file in the %USERPROFILE%\AppData\Roaming\.emacs.d\init.el because this is apparently the default behavior on Windows.

To troubleshoot this, I ran the following in the emacs *scratch* buffer.

user-emacs-directory
"~/.emacs.d/"

When I saw user-emacs-directory was ~/.emacs.d, I simply moved my .emacs.el file to %USERPROFILE%\.emacs.d\init.el. But this still didn't work. I continued with expand-file-name as shown below:

(expand-file-name user-emacs-directory)
"c:/Users/pats/AppData/Roaming/.emacs.d/"

Got to love how Windows works. (not) So I moved my emacs.el file to the %USERPROFILE%\AppData\Roaming\.emacs.d\init.el and this worked. The file was now being read. But I got other errors because my initialization file loaded other (personal emacs) files (in ~/myenv/emacs/*.el.

Warning (initialization): An error occurred while loading ‘c:/Users/pats/AppData/Roaming/.emacs.d/init.el’:

Hum... Seems like all my files ~/myenv/emacs/*.el would need to be moved in order for this to work but I didn't want to do that. Then I realized that because the HOME environment variable was not set, emacs was performing its default behavior.

SOLUTION

Once I set my windows HOME environment variable to %USERPROFILE% everything began to work like it has for the past 25 years. :-)

To set the HOME environment variable, I typed WindowsKey+"edit environment variables for your account" to open the Environment Variables dialog box, and entered HOME=%USERPROFILE%.

Now my emacs initialization file .emacs.el is is back to its rightful place $HOME/.emacs.el and not in %USERPROFILE%\AppData\Roaming\.emacs.d\init.el

To be fair, if Windows had just one place to put files for user installed packages the solution of making HOME=%USERPROFILE\AppData\Roaming might be acceptable, but because some applications use %USERPROFILE%, some use %USERPROFILE\AppData\Roaming and others use %USERPROFILE\AppData\Local it just makes it difficult to know where to find your configuration files.

I prefer having everything in my %USERPROFILE% or $HOME directory.

Another similar question was here:

PatS
  • 8,833
  • 12
  • 57
  • 100
  • 1
    "Got to love how Windows works. (not)" -- This issue doesn't have anything to do with Windows other than this is what Emacs developers have done to people who run Emacs on Windows. – Chris Charabaruk Dec 01 '18 at 22:43
  • 1
    I complain about developer decision here: https://emacs.stackexchange.com/questions/74983/why-emacs-uses-appdata-for-users-home-directory-on-windows – gavenkoa Dec 15 '22 at 12:20
  • I didn't know there was an emacs stackexchange. Thanks +1 – PatS Dec 18 '22 at 00:22
2

As kanja answered, the path to this file is stored in the user-init-file variable (or if no init file exists, the variable contains the default value for where to create it).

So regardless of which of the possible init file names you are using, and which directory it is in, you should be able to visit your init file with:

M-: (find-file user-init-file) RET

Or display its full path in the echo area with:

M-: (expand-file-name user-init-file) RET

phils
  • 71,335
  • 11
  • 153
  • 198
2

On Emacs 23 and Windows 7 it only works if you set:

HKCU\SOFTWARE\GNU\Emacs\HOME
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ex_
  • 39
  • 2
2

After Emacs 27.1, emacs has started respecting the $XDG_CONFIG_HOME. The init file or the init directory can now be found in $XDG_CONFIG_HOME/emacs/init.el.

In Windows $XDG_CONFIG_HOME could translate to %LOCALAPPDATA%.

In any case you can use the following emacs variables to find out the location of the your initialization file by M-x eval-expression

user-init-file

or the emacs configuration directory

user-emacs-directory
ksinkar
  • 281
  • 2
  • 9
1

I've found that Emacs 22 will occasionally open either "C:\Documents and Settings\username\Application Data\.emacs", or just "C:\Documents and Settings\username\.emacs" on my XP machine. I haven't found an explanation for why it occasionally changes it's mind.

~ will always point to whatever the current instance of emacs thinks is HOME, but kanja's tip (C-h v user-init-file) will always tell you what ~/.emacs actually maps to.

remcycles
  • 1,246
  • 13
  • 14
  • 2
    I've now noticed that Windows will set the `HOME` environment variable differently, depending on how I start `runemacs.exe`. If I start it from within the Explorer, `HOME` is set to `C:\Documents and Settings\username`, but if I start it from the Start Menu, `HOME` is `C:\Documents and Settings\username\Application Data`. Strange. – remcycles Dec 07 '10 at 21:37
1

On Windows XP it's:

C:\Documents and Settings\yourusernamehere\Application Data\
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
polarbear
  • 12,425
  • 6
  • 29
  • 22
1

On Windows 8.1, if Emacs is started from Windows Explorer, a shortcut or from cmd console it uses C:\Users\<USER>\AppData\Roaming.emacs init file. When I start Emacs from PowerShell, Emacs looks for its init file in C:\Users\<USER> folder. The fix to this issue was to set the HOME user environment variable (Control Panel\System and Security\System->Advanced system settings->Advanced->Environment variables) to C:\Users\<USER>. After this change, no matter how I start Emacs, it uses the same init file (see the accepted answer of this question)

Community
  • 1
  • 1
kri
  • 494
  • 1
  • 6
  • 12
0

There is a list of directories based on your Windows version and extra information: http://www.gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html

fatihpense
  • 618
  • 9
  • 11
0

For WIndows7& Emacs26.3:


if HOME environment is set, then the .emacs file should be in that folder.

otherwise, it should be in c:\.


In both cases, if .emacs is not there, _emacs should be used.

This is because we cannot create .emacs file according to the windows file naming rules.(but we can download or copy it from somewhere else).

ZhaoGang
  • 4,491
  • 1
  • 27
  • 39