2

Let's say I want all words "bad" be red (#ff0000).

What should I add to init.el so the color of the string/word "bad" will turn red in all modes whether it is org mode or some other mode into this:

https://i.imgur.com/7WMwG1s.png

I would like to do it in every file I have open and if possible it should be real time without any manual evaluation etc.

Is it possible to achieve in Emacs without installing any plugin?

Drew
  • 29,895
  • 7
  • 74
  • 104
jilocoiner
  • 105
  • 5

2 Answers2

3

You can define a globalized minor mode that will be active in all your buffers quite easily, eg.

(defconst my-font-lock-keywords '(("\\_<bad\\_>" . font-lock-warning-face)))

(define-minor-mode my-font-lock-mode ""
  :init-value nil
  :lighter ""
  (if my-font-lock-mode
      (font-lock-add-keywords nil my-font-lock-keywords)
    (font-lock-remove-keywords nil my-font-lock-keywords))
  (font-lock-flush))

(define-global-minor-mode my-font-lock-global-mode my-font-lock-mode
  (lambda () (my-font-lock-mode)))

Then, activate it with M-x my-font-lock-global-mode (or in your init)

Rorschach
  • 31,301
  • 5
  • 78
  • 129
1

There is already a package called hl-todo (https://github.com/tarsius/hl-todo). You can configure which keywords are highlighted and which colors with hl-todo-keyword-faces). You can configure which modes it's active in with either mode hooks or hl-todo-activate-in-modes.

jpkotta
  • 9,237
  • 3
  • 29
  • 34