maintaining multiple emacs configurations at the same time
Asked Answered
E

6

17

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

Ernestoernestus answered 5/7, 2013 at 7:41 Comment(1)
Out of curiosity: What you would need, emacs-starter-kit for example can't provide?Brunei
A
8

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.

Abstemious answered 8/7, 2013 at 17:34 Comment(0)
M
6

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)

Mazarin answered 5/7, 2013 at 8:8 Comment(1)
I just tried doing this so I can use both the ~/.emacs.d/ I've configured myself, and emacs-for-clojure. It made me realize one potential problem, which is configuration files which hard-code "~/.emacs.d/foobar.el" in places, ignoring your change to user-emacs-directory and breaking the configuration. This problem could be avoided by instead using (concat user-emacs-directory "foobar.el"), so that the pathname depends on what you have user-emacs-directory set to. People should really take this into consideration when making configurations for other people to use.Landlord
B
5

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

Besides answered 4/2, 2019 at 2:3 Comment(0)
U
4

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")))
Uwton answered 5/7, 2013 at 14:15 Comment(4)
This could be useful if you could use a command-line argument to set one of those values as true.Landlord
you can try something like this emacs --eval '(setq *emacs-prelude-enabled* t)'Uwton
you could hide it behind an alias, or use the command-switch-alist to build something like emacs --startup prelude if it's the eval that concerns you.Uwton
Actually this is really elegant if you combine this with juanleon's answer and include setting the user-emacs-directory variable for each condition (and use the variable as the 2nd argument to (add-to-list) for DRY reasons), then load with command-switch-alist.Christology
C
4
  • 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.
Corymb answered 22/6, 2019 at 14:7 Comment(0)
G
1

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.

Guajardo answered 27/4, 2018 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.