Up to this point, I used to have a separate configuration for each major mode, e.g. init.org.el, init.c++.el etc. I would invoke Emacs using an one of the aliases like these:

alias emacs-org='emacs -q --load ~/.emacs.d/init.org.el'
alias emacs-cpp='emacs -q --load ~/.emacs.d/init.c++.el'

At last, I got tired of having to run a different emacs instance every time I need to work with a different major mode, so I would like to unify my configuration.

I am thinking that the easiest way to do this is to put into init.el a hook for each major mode to load the corresponding configuration. So, init.el would have lines of the form:

(add-hook 'org-mode-hook (load-file "init.org.el))
(add-hook 'c++-mode-hook (load-file "init.c++.el))

I like this approach for the minimal number of changes that it requires. Also, having a hook in init.el, spares me from the need to put a lot of hooks in the files of individual modes.

However, there seems to be a problem. Namely, once say init.c++.el is loaded, all the configuration changes it performed will remain even when I switch to another mode... Is this correct? Is it possible solve this problem? And, more generally, is this approach to organizing configuration sound?

  • The second arg to add-hook needs to be a function. – Drew Aug 25 '17 at 14:37
  • All the configuration changes it performed will remain... Yes and no. It depends on the "configuration changes". – Drew Aug 25 '17 at 14:40
  • You can arrange to have the hook function for a particular mode remove particular "configuration changes" that are inappropriate. This is all a bit nebulous because you don't give specifics. – Drew Aug 25 '17 at 14:42
  • 3
    Standard practice is to use your configuration to adjust the behaviour and features of particular modes, not all of emacs. It sounds like your configuration involves making global changes to Emacs, which leads to the problem you have with different modes clobbering each other's config. If you provide some details about what you're actually trying to do we can probably help you sort it out. – Tyler Aug 25 '17 at 17:51
  • @Tyler Suppose that init.org.el defines a function and binds a key to it using (define-key global-map etc.). Do I have to change each such occurrence to org-mode-map? Also, even if I do that, the function definition itself pollutes the name space... – AlwaysLearning Aug 26 '17 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.