16

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

zzkt
  • 443
  • 3
  • 14
Archit
  • 913
  • 1
  • 10
  • 20

6 Answers6

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)

juanleon
  • 9,220
  • 30
  • 41
  • 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. – Sotanaht Jan 02 '14 at 03:17
6

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.

andreas-h
  • 10,679
  • 18
  • 60
  • 78
abo-abo
  • 20,038
  • 3
  • 50
  • 71
5

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

Marco Wahl
  • 694
  • 7
  • 14
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")))
zzkt
  • 443
  • 3
  • 14
  • This could be useful if you could use a command-line argument to set one of those values as true. – Sotanaht Jan 02 '14 at 03:09
  • 1
    you can try something like this `emacs --eval '(setq *emacs-prelude-enabled* t)'` – zzkt Jan 03 '14 at 11:01
  • 1
    you could hide it behind an alias, or use the [command-switch-alist](https://www.gnu.org/software/emacs/manual/html_node/elisp/Command_002dLine-Arguments.html) to build something like `emacs --startup prelude` if it's the `eval` that concerns you. – zzkt Apr 26 '14 at 16:29
  • 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. – mike Aug 06 '15 at 03:28
3
  • 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.
elixon
  • 1,123
  • 12
  • 15
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.

tariqk
  • 323
  • 1
  • 2
  • 9