如何在指定的用户初始化文件和emacs目录下启动emacs?

5
我想要能够启动具有不同配置的多个emacs实例,并且还希望能够将相应的目录符号链接到~/.emacs.d(或在我的情况下是~/.config/emacs),而不需要忽略符号链接目录中的init.el文件或编写额外的lisp代码来处理此问题。在我列出可能但(对我来说)不太令人满意的解决方案之后,最后一部分会变得清晰明了:
  1. 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.

  2. Solution:

    alias emacs='emacs -q --load "/path/to/init.el"'
    

    Each init.el file begins like this, to correctly set up the user-init-file and user-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 using initial-major-mode).
      • You'll need to explicitly call package-initialize; it won't be done automatically
  3. 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, when after-init-hook is executed.

  4. 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-fileuser-emacs-directory提供任何命令行参数。

或者是我漏掉了什么?

编辑#1:真的。这让我感到困惑。我进一步查看了一下。人们正在处理配置文件以获得这样的行为。或者去月球解决这个问题。或者说天书。或者召唤魔鬼。或者其他什么。

  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'.
    

提醒自己:在向网络求助之前,先了解软件即将发生的变化。但在这种特殊情况下,仍然值得这么做。


也许可以使用chroot? - user14560339
1个回答

2

请查看chemacs2,这是您所需要的。


仍然听起来像巫术,但是我开始倾向于用一个~/.emacs文件来解决问题。所以...谢谢你的帮助。有人已经以比我更好的方式完成了这个巫术。 - LittleBigUser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接