123

I've normally used ~/.emacs for my config file, but I've noticed others using ~/.emacs.d/init.el. Are there advantages to the init.el location? Any disadvantages?

Dan
  • 32,584
  • 6
  • 98
  • 168
Larry Coleman
  • 1,465
  • 2
  • 11
  • 10
  • 4
    Emacs (or at least some versions of emacs) write stuff on .emacs automatically. – Vivi Sep 24 '15 at 03:23
  • 5
    @Vivi Unless you're talking about very old versions, Emacs writes things into `user-init-file`. This will be `.emacs` if you already had a `.emacs`, and `init.el` if you used that instead. – Resigned June 2023 Mar 26 '17 at 22:53
  • 1
    People focused on the advantages of `~/.emacs.d/init.el` neglecting the disadvantages. I'll answer that part: it has no disadvantages at all! – Omar Jul 27 '18 at 01:10
  • 2
    Tangentially, starting from release version 27.1 Emacs will prefer the XDG config path `~/.config/emacs/` over `~/.emacs.d/`. IIUC, if the former *exists*, the latter will not be looked at. – phils Sep 16 '19 at 21:56
  • @phils It's the other way around. `(info "(emacs) Find Init")` says "`~/.emacs.d` and `~/.emacs` are always preferred if they exist, which means that you must delete or rename them in order to use the XDG location". – Basil Jul 07 '20 at 11:41
  • @Basil, I think my comment may have been accurate when I wrote it (I recall the issue causing some confusion amongst users); but yes, I believe that was fixed, and that you are correct. – phils Jul 07 '20 at 20:03

6 Answers6

116

Yes, there are. Quoting lunaryorn on Reddit:

  • It keeps the $HOME directory uncluttered, because all of Emacs' files are neatly contained in a single directory. For the same reason, all modern Emacs packages write their caches, histories, etc. into ~/.emacs.d/.

  • It makes version control easier, especially if you split your init.el into different files in ~/.emacs.d. You can just commit init.el like every other file, and git clone /my/emacs/config.git ~/.emacs.d on a new system gives you all of your Emacs configuration, without having to link a file into $HOME.

nicael
  • 1,598
  • 2
  • 13
  • 20
  • 4
    How does it make version control easier? You can split your init file without using `.emacs.d`. – Gilles 'SO- stop being evil' Sep 23 '14 at 19:01
  • 4
    It doesn't make version control easier if you only have .emacs file, but if you have other emacs files in .emacs.d, it does. For example `custom` saves files into .emacs.d and if you keep init.el in their all of your version controlled emacs files are in one directory. – Ted Roden Sep 23 '14 at 19:38
  • 4
    By versioning your .emacs.d/ directory completely, you can also keep under version control the packages you install via packages.el (ELPA, MELPA…). This make easier to share your configuration between multiple computers and to find where a regression was introduced in your configuration (you track your dependencies). The bad side is that it makes a pretty heavy repository. – tuxella Sep 23 '14 at 20:40
  • 13
    Version control systems don't version just one file -- they want to version a folder. So you version `~/`, and add just your `.emacs`. But now, you have to make sure you don't commit any other files, so you .hgignore or .gitignore every other file in `~/`. But now you can't track any other files in `~/`, like `~/.bashrc` or `~/.bash_aliases`. – zck Sep 23 '14 at 21:38
  • 1
    Related: https://github.com/RichiH/vcsh – wchargin Jan 05 '16 at 05:40
39

One additional advantage: if everything is in .emacs.d, you can keep your Emacs configuration on Dropbox (or an alternative) and symlink the whole configuration to your home directory. That makes syncing across computers trivial.

shosti
  • 5,048
  • 26
  • 33
25

If all you need to set up Emacs the way you like is an init file, it doesn't make a difference if it's ~/.emacs or ~/.emacs.d/init.el. But it makes sense to create a directory the moment you start splitting your init file, or adding Emacs-related configs, a Cask file for example. Also, it's the place where you'll save the file with abbrev definitions, the file for custom options (M-x customize), etc.

Keep everything neatly tucked in ~/.emacs.d.

10

Emacs 27 will introduce a new initialisation file early-init-file under user-emacs-directory, namely at ~/.emacs.d/early-init.el. So a further benefit to using ~/.emacs.d/init.el instead of ~/.emacs for user-init-file is that the former will place both initialisation files under the same roof, for consistency.

Until Emacs 27 is released, you can find the documentation for this new feature in the following files of the Emacs source tree:

Basil
  • 12,019
  • 43
  • 69
3

Not sure if this really makes a difference in terms of speed, but you can byte-compile your setup more easily if your configuration is in .emacs.d/init.el(i.e. create .emacs.d/init.elc)

armando.sano
  • 151
  • 4
  • 2
    Note that `~/.emacs.el` is also a valid init filename. – phils Jan 24 '18 at 12:01
  • [Emacs manual](https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html#Init-File): Byte-compiling your init file is not recommended. It generally does not speed up startup very much, and often leads to problems when you forget to recompile the file. A better solution is to use the Emacs server to reduce the number of times you have to start Emacs (see [Emacs Server](https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server)). – legends2k Nov 28 '19 at 13:08
3

You use ~/.emacs.d/init.el and just symlink that file to ~/.emacs, just in case some other programs / plugins expect to find .emacs.

On a macOS or GNU/Linux system, the command would be:

ln -s ~/.emacs.d/init.el ~/.emacs

This way you get all the benefits of VC of your ~/emacs.d directory structure and you'll be able to use a shorter name when you need to edit the init.el file through ~/.emacs instead of typing ~/emacs.d/init.el

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • What are these mysterious "other programs / plugins" which "expect to find .emacs" ? I don't think that's a real concern. – phils Sep 16 '19 at 21:53
  • 1
    One example could be Quicklisp, if you do a (ql:add-to-init-file) it defaults to ~/.emacs – C. Sederqvist Sep 16 '19 at 22:47
  • 2
    How curious. I would say that any such cases would warrant bug reports -- Emacs has supported a variety of init file names for an extremely long time, and the priority sequence is clearly documented, so there's no good reason for other programs not to be getting that right. I wasn't anticipating an actual example at all, though :) Cheers. – phils Sep 17 '19 at 00:27