maintaining multiple emacs configurations at the same time

二次信任 提交于 2019-12-20 09:38:33

问题


I want to maintain multiple emacs configurations like emacs-prelude, emacs-starter-kit, and my own custom emacs configuration simultaneously on same user account on same pc.
for that i have setup directories like .emacs1.d, .emacs2.d, .emacs3.d.

Each emacs user directory has a init.el file that should be loaded on startup. Instead of .emacs file i prefer using init.el file.

How do i load these custom config directories?

I tried running emacs --eval '(setq user-emacs-directory "~/.emacs1.d/")'

it just sets the value of user-emacs-directory, but does not load the files from it


回答1:


I would try something like

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

And then you would do something like at the beginning of your init.el files:

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

(or you can also eval both things as command-line parameters)




回答2:


If you like to invoke things from console, I'd put this in .bashrc:

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

And then invoke them like so:

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

You even get completion in bash after the $ sign.

You can even alias those things like so:

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'

And the invoke them like so:

emacs1
emacs2
emacs3

Of course,

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

still has to be in each init.el.




回答3:


Alternatively, you could use a single ~/.emacs or init.el file and select which config directories to load.

(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")))



回答4:


As an extension of nik's answer, and their comment here, here's what I did, in the end:

;;; -*- 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

One thing I noted was that, if you're using stuff like spacemacs, it'll fail because what it's looking for isn't load-path but instead user-emacs-directory. Also, putting the load-path into the spacemacs folder made Emacs complain that load-path had your .emacs.d file, which would cause problems.

As it is, this works both with spacemacs and emacs-starter-kit. Haven't tried any other configurations, but I might start looking into that.




回答5:


chemacs is a piece of software to manage several Emacs configurations. See https://github.com/plexus/chemacs .




回答6:


  • Create new folder for your Emacs profile - e.g. /home/user/.emacs.d.vanilla
  • Create init.el inside it and copy following two lines in it. Those lines will tell Emacs to treat new init.el as main config file and its folder as main config folder:
(setq user-init-file (or load-file-name (buffer-file-name)))
(setq user-emacs-directory (file-name-directory user-init-file))

Now start emacs like this:

emacs -q -l /home/user/.emacs.d.vanilla/init.el
  • -q - skip the default ~/.emacs.d/init.el .
  • -l - load our special init.el that tells Emacs about new init folder and init file location.



回答7:


Symlinks might be great to choose from several configurations. See https://stackoverflow.com/a/21568966/5128179 .



来源:https://stackoverflow.com/questions/17483598/maintaining-multiple-emacs-configurations-at-the-same-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!