12

I have read the guide found here:

How Emacs Finds Your Init File

I cannot find any .emacs directory in my home directory. I have show hidden files selected. I also cannot find the init.el file in Emacs home directory.

Since I have been unable to find the init.el file, is it reasonable to think there isn't one?

I am using Emacs 27.1 on Windows 10. Except for Emacs home directory I installed with the defaults. I used no command-line options during install.

I seem to remember I have once read a post which explained Emacs has an internal variable which is used to hold the value of the location of where Emacs found the init.el file after it starts, but after searches I have been unable to find what that variable is.

How can I find my init.el file, having failed to locate it in the locations mentioned in the manual?

Does a variable exist which says where the init.el file was found? If so, what is it?

Drew
  • 75,699
  • 9
  • 109
  • 225
Georgina Davenport
  • 301
  • 1
  • 2
  • 9
  • 2
    You might like to clarify why you think there should be a `.emacs` *directory* (AFAIK there can be a `.emacs` *file* or a `.emacs.d` directory, but I've never heard of a `.emacs` directory). Similarly clarify what you mean by "Emacs home directory". – Stefan Aug 14 '20 at 03:19
  • 1
    `C-h v` and then `user-init-file` will show you what your current user init file is – Cousin Dupree Aug 14 '20 at 14:08
  • Thanks @minibuffer. I have looked at the variable and it says "Its value is "~/.emacs"". I cannot fine a file of that name. What does this mean? – Georgina Davenport Aug 14 '20 at 16:35
  • That is because windows (usually) sets $HOME as %APPDATA% and puts your `.emacs` file in there. So in Windows ~ usually meanss %APPDATA%. In a windows cmd prompt do `cd %APPDATA%`. You will most likely find your `.emacs` file in there. – Cousin Dupree Aug 14 '20 at 17:29
  • @minubuffer. Excellent! Yes, I have found the `emacs.d` directory there. In it are the `auto-save-list` directory and the `elpa` directory. In `elpa` there is `archives\gnu\archive-conitents`. Why can't I find an init file here though? – Georgina Davenport Aug 14 '20 at 17:37
  • 1
    well you might not have an `.emacs` file. Create an `init.el` file *inside* the `.emacs.d` directory that you just found. put some sample config on there and see if emacs picks it up. For example `(setq initial-scratch-message "Hello from my new init file :)")` – Cousin Dupree Aug 14 '20 at 17:44
  • @minibuffer, you're a genius. Thank you! That really, really helped. My next question was to be "Now how do I make a test file?", and you preempted it. Yes, I tried your idea. I wasn't sure where the message would appear at first but I soon found it in the *scratch* buffer. Great! I am now ready to start learning elisp. Thanks a lot! :) – Georgina Davenport Aug 14 '20 at 20:47
  • @minibuffer, how do I accept your answer as the one I was looking for? – Georgina Davenport Aug 14 '20 at 22:20
  • Glad it helped. I will consolidate the comments into an answer when I have a bit of time. – Cousin Dupree Aug 15 '20 at 00:01
  • you can customize the emacs theme from the option menu and also save the option. start over emacs with new theme, and look for .init or .emacs in .emacs.d folder. – Shahab Rasekh Aug 16 '21 at 07:30
  • https://stackoverflow.com/a/10545955/324105 might also prove useful for this question. – phils Sep 15 '21 at 14:42

3 Answers3

13

This answer specifically address the question of how/where to find Emacs' user-init-file on Microsoft Windows.

HOME and Startup Directories on MS-Windows

Here is the relavent section from the Emacs manual (available via C-h i) Emacs > Microsoft Windows > Windows Home

The Windows equivalent of HOME is the user-specific application data directory. The actual location depends on the Windows version; ... C:\Users\USERNAME\AppData\Roaming on Windows Vista and later ... If this directory does not exist or cannot be accessed, Emacs falls back to C:\ as the default value of ‘HOME’.

This location is stored in the environment variable that windows refers to as %APPDATA%. See this question and answer on superuser

You can try to find the location of your user-init-file by doing C-h v and then user-init-file [RET]. You will see an output similar to

~/.emacs

On Windows this isn't particularly helpful. We can now find out what Emacs means by ~ doing one of the following:

C-x C-f > ~ > [backspace] The echo area at the bottom of your Emacs screen will expand to show you what Emacs currently consider to be HOME. (Note that this works on Windows and according to @NickD doesn't work on GNU/Linux)

OR more generally (quoting from the Emacs manual)

You can always find out what Emacs thinks is your home directory’s location by typing C-x d ~/ <RET>. This should present the list of files in the home directory, and show its full name on the first line. Likewise, to visit your init file, type C-x C-f ~/.emacs <RET> (assuming the file’s name is .emacs).


Setting up the init.el on Windows

Instead of keeping a .emacs file most emacs users now use an init.el file stored in your user-emacs-directory. This directory defaults to ~/.emacs.d and in your case %APPDATA%/.emacs.d/. Assuming you don't already have a .emacs file stored in %APPDATA%/ You can now go ahead and create an init.el file inside the .emacs.d directory. Put some sample configuration in your init.el file and see if Emacs picks it up when you re-start Emacs. Also see this this answer on Emacs Stackexchange.

Cousin Dupree
  • 358
  • 3
  • 12
  • Thank you for this comprehensive solution, minibuffer. I had wondered one thing though, like NickD I don't know if I have understood the [backspace] instruction, or if it works differently for the Windows default. When I press backspace, it only deletes the tilde. – Georgina Davenport Aug 18 '20 at 02:01
  • Well on my keyboard when I hit backspace on `~` in the minibuffer it expands to show me the absolute path. In any case, you have an alternate way to achieve the same. – Cousin Dupree Aug 18 '20 at 02:36
  • Yes, once you mentioned `%APPDATA%`, I merely typed `echo %APPDATA%` on the command line, and on seeing the variable was defined, I did `cd %APPDATA%` to visit the directory and found the `.emacs.d` directory there. In it I put the test `init.el` file you recommended, which contains only `(setq initial-scratch-message "Hello from my new init file :)")` and I then launched emacs and found the test message in the scratch buffer, confirming the process works, at least for me. Thank you again for your help! – Georgina Davenport Aug 18 '20 at 14:34
  • Thanks very much for this, very helpful. However, not sure if this variable changed recently or not but on Emacs 27.1 its ```user-emacs-directory``` rather than ```emacs-user-directory```. – Marco Craveiro Sep 15 '21 at 07:16
  • 1
    @MarcoCraveiro: You are right. It should be `user-emacs-directory`. Fixed. – Cousin Dupree Sep 15 '21 at 14:28
8

The variable is user-init-file. Its doc string says:

File name, including directory, of user’s initialization file. If the file loaded had extension ‘.elc’, and the corresponding source file exists, this variable contains the name of source file, suitable for use by functions like ‘custom-save-all’ which edit the init file. While Emacs loads and evaluates any init file, value is the real name of the file, regardless of whether or not it has the ‘.elc’ extension.

EDIT: If the value of the variable is non-nil, as in your case, it means that it actually found the file ~/.emacs somewhere, somehow. I don't know much about Windows, but if you look in what you think is your home directory and cannot find a file called .emacs, it might be that some translation is going on. ISTR, that in some cases, on Windows, the file name was _emacs, but that was a long time ago, in a universe far, far away, and I may very well be wrong.

See if this link helps: it describes how the HOME directory is determined on Windows.

Also, the method that @minibuffer describes in a comment might well be the best way to find the file: do C-x C-f ~/ RET and look at the pathname at the top of the directory listing (this is slightly different from what the comment describes, but I couldn't get the backspace method to work for me: it just deletes the ~ in the prompt - but this is on Linux, and Windows may behave differently).

NickD
  • 27,023
  • 3
  • 23
  • 42
  • Thank you. It says "Its value is "~/.emacs". I have looked in my home directory and I cannot find a file with that name. What does this mean? – Georgina Davenport Aug 14 '20 at 16:30
  • 1
    In addition to my earlier comment you can also do `C-x C-f` and then `~` and then hit `backspace` this will expand to show you the absolute path of what emacs means by `~`. – Cousin Dupree Aug 14 '20 at 17:35
  • @minibuffer. Yes, I can do that! This is very helpful. Thank you. I can go directly to the directory within emacs without having to search for it. In that directory structure though I still cannot find the init file. Why is this? – Georgina Davenport Aug 14 '20 at 17:42
1

You can create or open an an existing init file when opening ~/.emacs or ~/.emacs.d/init.el. ~ is a macro character which expand to the correct directory. On a fresh install you have to create an init file.

gigiair
  • 2,124
  • 1
  • 8
  • 14
  • Your answer may mean currently there is no init file. I didn't create one. I assumed there would be a default copy. Is it reasonable to believe as I haven't created one that there is no emacs init file on my computer? – Georgina Davenport Aug 14 '20 at 16:33