Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable a non-global minor mode by default, on emacs startup?

Tags:

emacs

mode

I want to enable rainbow-mode everytime I start emacs, rather than having to use M-x rainbow-mode.

I guess there is some command I put in my .emacs file.

I tried all of the following, but none of them worked:

(require 'rainbow-mode)     (rainbow-mode initialize)  (global-rainbow-mode) 

More generally, how do I load any mode/package automatically on startup?

like image 452
Dark Knight Avatar asked Apr 16 '13 22:04

Dark Knight


People also ask

How do I enable minor mode in Emacs?

If you want to enable/disable minor-modes on the whole session, press [e] or [d] on any minor-mode to keep its status even if you change major-modes, buffers, files. It continues until stopping emacs. This enable/disable list is stored in the global of manage-minor-mode-default .

How do I set mode in Emacs?

Usually, the major mode is automatically set by Emacs, when you first visit a file or create a buffer (see Choosing File Modes). You can explicitly select a new major mode by using an M-x command.

What is major and minor mode in Emacs?

A mode is a set of definitions that customize Emacs behavior in useful ways. There are two varieties of modes: minor modes, which provide features that users can turn on and off while editing; and major modes, which are used for editing or interacting with a particular kind of text.


1 Answers

rainbow-mode isn't a global minor mode, so it needs to be enabled on a per-buffer basis.

I only use it for CSS, so I have:

(add-hook 'css-mode-hook 'my-css-mode-hook) (defun my-css-mode-hook ()   (rainbow-mode 1)) 

If you genuinely want it to be global, everywhere, you can easily define a global minor mode yourself:

(define-globalized-minor-mode my-global-rainbow-mode rainbow-mode   (lambda () (rainbow-mode 1)))  (my-global-rainbow-mode 1) 

You can add any arbitrary logic to that (lambda () (rainbow-mode 1)) function (which will be evaluated in every buffer) in order to decide whether or not to actually call (rainbow-mode 1) for a given buffer, so if you're comfortable with elisp then you can easily extend this approach to cover your specific requirements for the mode in question.


More generally, how do I load any mode/package automatically on startup?

It can vary, but the approaches I've shown would suffice for most minor modes: Either you want them enabled whenever MODE is enabled (being some specific other mode name), in which case you can use the MODE-hook variable (which will always be available) as per the css-mode-hook example; or else you want the mode enabled permanently, in which case a global minor mode is a good approach (because you can toggle it on and off globally). Some minor modes are global by default (or provide global variants), but you can create your own if necessary, as per the my-global-rainbow-mode example.

Also be aware that modes can be derived from other modes, in which case all relevant MODE-hook hooks will be run (for details see https://stackoverflow.com/a/19295380/324105). A common use-case is to use prog-mode-hook to enable functionality wanted for all the programming modes which are derived from it (which is most programming modes).

Remember that many (hopefully most) libraries and packages will provide usage instructions. If you can't find documentation, be sure to try M-x find-library to visit the library file, and then read through the comments at the top. There is often a very informative "Commentary" section, and sometimes this is the primary source of end-user documentation, and explain how to enable its functionality.

like image 185
phils Avatar answered Sep 23 '22 15:09

phils