I want to color numbers differently from the text characters in emacs for any mode that emacs is in. How do I modify the .emacs file for this purpose? I would like my numbers to always show up in pink.

Gvim seems to have this feature built in already.

  • It is late at night here where I am and I've only tested this one time: (add-hook 'text-mode-hook (lambda () (font-lock-add-keywords nil (list (list "\\([0-9]+\\)" '(0 font-lock-warning-face t)) )))) That should put you in the right direction -- the hook is for the major mode text-mode. You can adjust the regexp as needed. – lawlist May 30 '14 at 7:43
  • 1
    There's a highlight-numbers mode on melpa and also available here. – amitp Jul 24 '14 at 15:11

Usage -- activate or deactivate locally with:   M-x smilingbuddha-mode

To enable smilingbuddha-mode globally, either uncomment (global-smilingbuddha-mode) and restart; or, type M-x global-smilingbuddha-mode (which will remain in effect until you restart).

Please feel free to modify the regexp to suit your personal needs.

(define-minor-mode smilingbuddha-mode
  "Minor mode for highlighting numbers."
  :group 'smilingbuddha
  :lighter " :)"
      (when (not (or font-lock-mode global-font-lock-mode))
        (font-lock-mode 1))
      (font-lock-add-keywords nil
        (list (list "\\([0-9]+\\)" '(0 'smilingbuddha-pink-face t)) ))
      (message "Turned ON `smilingbuddha-mode`."))
    ((not smilingbuddha-mode)
      (font-lock-remove-keywords nil
        (list (list "\\([0-9]+\\)" '(0 'smilingbuddha-pink-face t)) ))
      (message "Turned OFF `smilingbuddha-mode`."))))

(defgroup smilingbuddha nil
  "Highlight numbers."
  :version "0.1"
  :group 'smilingbuddha)

(defface smilingbuddha-pink-face
  '((t (:foreground "black" :background "pink")))
  "Face for `smilingbuddha-pink-face`."
  :group 'smilingbuddha)

(defun turn-on-smilingbuddha-mode ()
  (smilingbuddha-mode 1))

(defun turn-off-smilingbuddha-mode ()
  (smilingbuddha-mode -1))

(define-globalized-minor-mode global-smilingbuddha-mode
  smilingbuddha-mode turn-on-smilingbuddha-mode)
;; (global-smilingbuddha-mode)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.