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


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)


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


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-hookexample; 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-hookhooks will be run (for details see 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.


Set Color

[Originally Posted By]:

This is one of the ways to change foreground and background colors. See HowtoChangeFrameColors for others.

You can use M-x set-foreground-color and M-x set-background-color interactively to set the colors for the selected frame.

If you use these in your .emacs file, the colors will only be changed for the first frame.


 (set-foreground-color "white")
 (set-background-color "blue")

Note that the colors must be strings – and there must be a mapping from the name to a RGB value somewhere on your system. One of these places is typically the /usr/X11/include/rgb.txt file. Instead of naming the colors, you can also use the RGB values directly:

 (set-foreground-color "#FFF")
 (set-background-color "#0000FF")

These RGB values cannot be entered interactively, because the interactive input checks wether the names are defined. The RGB values can have 3, 6, or 9 hex digits.

1. To enter RGB values interactively, just use `M-: (set-background-color "000000FFF")’, or whatever.

2. To find a color interactively, try ColorPalette. You can also use the color palette to analyze color components in color models RGB and HSV or to convert between them.

3. See library Lisp:doremi-frm.el for commands to convert and tweak colors:

  • `doremi-color-name-to-RGB’ – Return the hexadecimal RGB code for a given color name. Provides Completionbased on all existing color names.
  • `doremi-increment-color-component’ – Increment any component (red, green, blue, hue, saturation, value) of a given color. This returns another color that is redder, greener,…, or brighter than a given color by some amount.

4. See library Lisp:hexrgb.el for functions to do the following:

  • convert between decimal and hex RGB values
  • convert between RGB and HSV (hue, saturation, value) component values
  • increment hex RGB color specs such as #FFAD39C40A16

5. You can set foreground and background Face color interactively by incrementing/decrementing the current color components (RGB, HSV, or CMY: cyan, magenta, yellow). You can do that using the arrow keys or mouse wheel with library Lisp:doremi-frm.el, commands ‘doremi-face-bg-rgb’ and ‘doremi-face-fg-rgb’. Just hold down an arrow key to continue modifying the color; stop when you get the face color you like – WYSIWYG. See DoReMi.

6. At first <someone> misread the statement above to mean that “there must be a mapping” between a name and an RGB value in order for you to use a particular RGB value, which is incorrect: any hex RGB values can be used. The statement above is, however, correct: in order to use a given name, that name must map to RGB.

Changing highlight line color in emacs

[Originally Posted By]:

That would be an easy fix if you customize your init file (~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el)

Turn on hl-line:

(global-hl-line-mode 1)

Set any color as the background face of the current line:

(set-face-background 'hl-line "#3e4446")

To keep syntax highlighting in the current line:

(set-face-foreground 'highlight nil)
shareimprove this answer