2

The Emacs manual often talk about init.el and .emacs interchangeably, generally referring to them as "the Init File" like so:

When you start Emacs, it normally attempts to load your init file. This is either a file named .emacs or .emacs.el in your home directory, or a file named init.el in a subdirectory named .emacs.d in your home directory.

I try to put all my customization code into init.el because, according to this site, it is 'modern' and 'preferred':

On linux and Mac, by default, emacs look for init file at

~/.emacs
~/.emacs.el
~/.emacs.d/init.el (this is modern, preferred, since ~2010)

I also try not to manually edit .emacs too often because there are custom-set-variables sitting in the file and there is a warning that says:

 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.

Other advantages of using the init.el over .emacs can be found here.

But I recently realize that some of the code I put in init.el have been left unevaluated when Emacs is restarted. So I have to call up the file and M-x eval-buffer every time I restart.

Therefore, I'd like to ask:

  1. What is the difference between init.el and .emacs as far as customization is concerned? (question has been partly answered here.)

  2. What is the point of keeping two (or more) Init files when having one would do? Some people even talked about splitting init.el into different files, quoting lunaryorn on Reddit:

It makes version control easier, especially if you split your init.el into different files in ~/.emacs.d.

  1. What to do when (some) code in one of them are persistently left unevaluated?

Updates:

I've deleted .emacs and moved all its contents into init.el, but part of the code in init.el ,such as the one below, is still unevaluated everytime emacs restarts.

;;set Chinese table alignment font size

(set-face-attribute
 'default nil
 :font (font-spec :name "-*-Menlo-normal-normal-normal-*-*-*-*-*-m-0-iso10646-1"
                  :weight 'normal
                  :slant 'normal
                  :size 24))
(dolist (charset '(kana han symbol cjk-misc bopomofo))
  (set-fontset-font
   (frame-parameter nil 'font)
   charset
   (font-spec :name "-*-STFangsong-normal-normal-normal-*-*-*-*-*-p-0-iso10646-1"
              :weight 'normal
              :slant 'normal
              :size 28)))

Sati
  • 775
  • 6
  • 21
  • 1
    The difference between these files is precedence. IIRC, `.emacs` is checked before `init.el`. Since `init.el` resides in a directory devoted to Emacs configuration, it makes sense to use it instead, since it makes things like version controlling your init file easier. –  Jul 12 '19 at 18:11
  • 1
    If you've decided to use `init.el` then you need to delete `.emacs` (after copying its contents to the newer file), otherwise Emacs will continue to use `.emacs` as your init file. In short, put *all* of your init code in one file, and delete all other would-be init files. – phils Jul 13 '19 at 00:37
  • @phils, how about `custom-set-variables`, does that have to go into `init.el` as well? – Sati Jul 13 '19 at 00:59
  • 1
    @DoMiNeLa10♦, This is not exactly a duplicate. The question you highlighted just talks about why using `init.el` is better than `.emacs`. It doesn't say much about what to do with `.emacs` when you decide to use `init.el` instead. – Sati Jul 13 '19 at 01:14
  • @Sati `custom-set-variables` is being written to `.emacs` only because that is your init file. If your init file was `init.el` then Emacs would be writing it to that. So yes, you should move *everything* in `.emacs` to `init.el` and delete `.emacs`. If a `~/.emacs` file even *exists*, then that is your init file. Emacs may update that "custom" config when you exit, so I suggest you (1) Exit emacs; (2) Run `emacs -q ~/.emacs.d/init.el ~/.emacs` and copy/paste the .emacs contents to the init.el file, save that file, and exit; (3) Delete the .emacs file; (4) Start emacs normally. – phils Jul 13 '19 at 03:45
  • 1
    @Sati You *can* put that `custom-set-variables` section in another file (e.g. `~/.emacs.d/custom.el`) if you find that tidier. See `C-h v custom-file`. Note that your init file will need to both specify the file name *and* load it. – phils Jul 13 '19 at 03:46
  • @phils, Can you show me how to do it? I just got into some trouble after deleting `.emacs`. – Sati Jul 13 '19 at 03:52
  • Does `emacs --debug-init` show you any error / backtrace? – phils Jul 13 '19 at 05:12
  • No. I just get the `local variables list is not properly terminated` message every time I open `init.el`. – Sati Jul 13 '19 at 05:15
  • I seem to get a reset of all custom settings to the default every time a _new window_ is loaded. `C-x 5 2` reverts all custom settings back to the default. – Sati Jul 13 '19 at 05:24
  • 1
    Well fix your `Local Variables:` list for starters. If you have no idea what that is or why you're getting that error, then you can insert a page break character at the end of the file with `C-q C-l` (the character will look like `^L`), as emacs only looks for a Local Variables list in the final page of the file. – phils Jul 13 '19 at 05:34
  • `C-x 5 2` will absolutely not revert "all custom settings" back to the default. *Certain settings* are frame-specific, however (e.g. frame parameters and terminal-local variables). That is a completely different question, though. – phils Jul 13 '19 at 05:43
  • Okay, `Local Variables:` list has been fixed by inserting an `End:` after it. Still doesn't fix the evaluation problem, though. Seems to me that only _some_ of the code in `init.el` gets evaluated - even when `.emacs` is not around - but everything in `.emacs` would be evaluated upon startup. – Sati Jul 13 '19 at 05:53
  • 1
    The init file name doesn't have an effect on how Emacs evaluates the init file. Your issue indicates that there is some kind of problem with your init code. Try to find a minimum test case for something which isn't working (e.g. rename your new init.el file to a backup name, and create a new init.el file which contains only non-working code), and then you can post a new question about that. – phils Jul 13 '19 at 07:40

0 Answers0