同时维护多个emacs配置

  • 本文关键字:emacs 配置 维护 emacs elisp
  • 更新时间 :
  • 英文 :


我想在同一台电脑上的同一用户帐户上同时维护多个emacs配置,如emacs prelude、emacs starter kit和我自己的自定义emacs配置。
为此,我有.emacs1.d、.emacs2.d和.emacs3.d等安装目录。

每个emacs用户目录都有一个init.el文件,应该在启动时加载。我更喜欢使用init.el文件,而不是.emacs文件。

如何加载这些自定义配置目录?

我试着运行emacs --eval '(setq user-emacs-directory "~/.emacs1.d/")'

它只设置用户emacs目录的值,但不从中加载文件

我会尝试类似的东西

emacs -q --eval '(load-file "~/.emacs1.d/init.el")'

然后你会在init.el文件的开头做一些类似的事情:

(setq user-emacs-directory "~/.emacs1.d/")

(或者您也可以将这两件事作为命令行参数进行评估)

如果你想从控制台调用东西,我会把它放在.bashrc:中

export emacs1=~/.emacs1.d/init.el
export emacs2=~/.emacs2.d/init.el
export emacs3=~/.emacs3.d/init.el

然后像这样调用它们:

emacs -q -l $emacs1
emacs -q -l $emacs2
emacs -q -l $emacs3

你甚至可以在$sign.后的bash中完成

你甚至可以这样别名这些东西:

alias emacs1='emacs -q -l ~/.emacs1.d/init.el'
alias emacs2='emacs -q -l ~/.emacs2.d/init.el'
alias emacs3='emacs -q -l ~/.emacs3.d/init.el'

他们这样调用它们:

emacs1
emacs2
emacs3

当然,

(setq user-emacs-directory "~/.emacs1.d/")

仍然必须在每个CCD_ 2中。

chemacs是一个用于管理多个Emacs配置的软件。看见https://github.com/plexus/chemacs。

或者,您可以使用单个~/.emacs或init.el文件并选择要加载的配置目录。

(defvar *emacs-prelude-enabled* t)
(defvar *emacs-starter-enabled* nil)
(defvar *other-config-enabled* nil)
(cond (*emacs-prelude-enabled* 
       (add-to-list 'load-path "~/.emacs1.d/")
       (load "~/.emacs1.d/init.el"))
      (*emacs-starter-enabled* 
       (add-to-list 'load-path "~/.emacs2.d/")
       (load "~/.emacs2.d/init.el"))
      (*other-config-enabled*
       (add-to-list 'load-path "~/.emacs3.d/")
       (load "~/.emacs3.d/init.el")))
  • 为您的Emacs配置文件创建新文件夹,例如/home/user/.emacs.d.vanilla
  • 在其中创建init.el,并在其中复制以下两行。这些行将告诉Emacs将新的init.el视为主配置文件,将其文件夹视为主配置文件夹:
(setq user-init-file (or load-file-name (buffer-file-name)))
(setq user-emacs-directory (file-name-directory user-init-file))

现在开始这样的emacs:

emacs -q -l /home/user/.emacs.d.vanilla/init.el
  • -q-跳过默认的~/.emacs.d/init.el
  • -l-加载我们特殊的init.el,它告诉Emacs新的init文件夹和init文件位置

作为nik的答案和他们在这里的评论的扩展,以下是我所做的,最后:

;;; -*- lexical-binding: t -*-
;;
;; Added to appease the package.el gods
;; (package-initialize)
;; Select the profile based on which command-line argument used
(defvar *emacs-config-switcher/profiles-alist* nil
  "An alist for the profiles that are registered here")
(defun emacs-config-switcher/register-profile (key path &optional file)
  "Register profiles to global variable, referenced by KEY.
PATH points to the directory where the profile is stored. By default, will use init.el,
but it can be specified using FILE."
  (or file (setq file "init.el"))
  (setq *emacs-config-switcher/profiles-alist* (cons (cons key (list
                                                                :directory (file-name-as-directory path)
                                                                :file (expand-file-name file path)))
                                                *emacs-config-switcher/profiles-alist*)))
(defun emacs-config-switcher/load-profile (switch)
  "Load profile based on key."
  (let ((key (pop command-line-args-left)))
    (if (assoc key *emacs-config-switcher/profiles-alist*)
    (progn (let ((directory-path
              (plist-get (cdr (assoc key *emacs-config-switcher/profiles-alist*)) :directory))
             (init-file
              (plist-get (cdr (assoc key *emacs-config-switcher/profiles-alist*)) :file)))
         (setq user-emacs-directory directory-path)
         (load init-file)))
      (error "Profile %s does not exist." key))))
; Register profiles here
(emacs-config-switcher/register-profile "emacs-starter-kit" "~/emacs-profiles/emacs24-starter-kit")
(emacs-config-switcher/register-profile "spacemacs" "~/emacs-profiles/spacemacs")
; Add the custom switches
(add-to-list 'command-switch-alist '("-S" . emacs-config-switcher/load-profile))
(add-to-list 'command-switch-alist '("--startup" . emacs-config-switcher/load-profile))
;;; init.el ends here

我注意到的一件事是,如果你使用像spacemacs这样的东西,它会失败,因为它要找的不是load-path,而是user-emacs-directory。此外,将load-path放入spacemacs文件夹会让Emacs抱怨load-path有您的.emacs.d文件,这会导致问题。

事实上,这适用于spacemacs和emacs入门套件。我还没有尝试过任何其他配置,但我可能会开始研究它。

最新更新