当有多个 emacs init 文件时会发生什么?

What happens when there are multiple emacs init files?

好奇的问题...

假设我有以下文件...~/.emacs~/.emacs.d/init.el~/.emacs.el.

现在,假设每一个都包含单独的代码片段,例如:

~/.emacs
(global-set-key (kbd "<f8>") 'execute-extended-command)

~/.emacs.d/init.el
(global-set-key (kbd "<apps>") 'execute-extended-command)

~/.emacs.el
(global-set-key (kbd "<menu>") 'execute-extended-command)

请注意,所有文件都对 execute-extended-command 进行了单独更改。当打开 emacs 时,将执行这些文件中的哪些?全部,其中之一,还是 none 个?有没有先执行的特殊顺序?此外,拥有多个初始化文件是不是一个坏主意?

任何涉及这些主题的答案,以及任何额外的信息都是足够的,我只是想知道在这种情况下会发生什么。

来自 the manual:

49.4.4 How Emacs Finds Your Init File

Normally Emacs uses the environment variable HOME (see HOME) to find .emacs; that's what ‘~’ means in a file name. If .emacs is not found inside ~/ (nor .emacs.el), Emacs looks for ~/.emacs.d/init.el (which, like ~/.emacs.el, can be byte-compiled).

所以~/.emacs.d/init.el只有在其他两个找不到的情况下才会参考。快速测试表明,当两者都可用时,~/.emacs.el 优于 ~/.emacs

您应该选择其中一个位置并始终如一地使用它。

我个人喜欢 ~/.emacs.d/init.el 因为 Emacs 将其他东西放在该目录中(例如 custom.elelpa/)并且许多 Emacs 包从该目录加载它们自己的配置文件。