~/.emacs.d
(或在我的情况下是~/.config/emacs
),而不需要忽略符号链接目录中的init.el
文件或编写额外的lisp代码来处理此问题。在我列出可能但(对我来说)不太令人满意的解决方案之后,最后一部分会变得清晰明了:
Solution:
env HOME=$HOME/path/to/a/pseudo/home/directory emacs
Found at How to start up emacs with different configurations?
Problem:
The down-side to this technique is that Emacs won't see all your other dot files (because it will be looking in
$HOME
), and so running other processes from within Emacs won't necessarily work as normal; but that's not likely to be a huge issue if you're just experimenting, and you can always symlink or copy the bits you need.Solution:
alias emacs='emacs -q --load "/path/to/init.el"'
Each
init.el
file begins like this, to correctly set up theuser-init-file
anduser-emacs-directory
variables:(setq user-init-file (or load-file-name (buffer-file-name))) (setq user-emacs-directory (file-name-directory user-init-file))
Found at How to start emacs with a custom user-emacs-directory?
Problem:
- it breaks
emacs-init-time
[...]. - it is not equivalent to a normal startup [...]:
after-init-hook
is run before the init file is loaded.- The
*scratch*
buffer is created before the init file is loaded. You'll have to change its mode explicitly (instead of usinginitial-major-mode
). - You'll need to explicitly call
package-initialize
; it won't be done automatically
- it breaks
Perhaps a Solution:The following doesn't work.
alias emacs='emacs -q --eval "(progn (setq user-init-file /path/to/init.el) (setq user-emacs-directory (file-name-directory user-init-file)))"
But I have no clue, if something like this would really work and solve the problems with the solutions above. I couldn't find when--eval
is executed in the startup process. I also don't know, how I could test, whenafter-init-hook
is executed.Solution (Edit #2):
Another solution -- mentioned in some threads (dropped the link, but google helps) -- is to use the standard
init.el
to write a switch, for example controlled by a dedicated environment variable, which than loads the corresponding init.el and sets the user-emacs-directory.Tianshu Wang mentioned chemacs2, what does exactly that via a command line argument, instead of an environment variable.
For my part, I will take that way, bacause it handles also extra config files for example created by spacemacs. But see also solution 5.
让我感到困惑的是,emacs没有为user-init-file
和user-emacs-directory
提供任何命令行参数。
或者是我漏掉了什么?
编辑#1:真的。这让我感到困惑。我进一步查看了一下。人们正在处理配置文件以获得这样的行为。或者去月球解决这个问题。或者说天书。或者召唤魔鬼。或者其他什么。
Solution (Edit #3):
Use git version of emacs or wait for official release emacs29.1
Changlog for emacs on git:
* Startup Changes in Emacs 29.1 +++ ** Emacs now supports setting 'user-emacs-directory' via '--init-directory'.
提醒自己:在向网络求助之前,先了解软件即将发生的变化。但在这种特殊情况下,仍然值得这么做。