Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorize snippets of text in emacs

Tags:

emacs

Suppose I have a few words I would like to highlight, so I want to change the color of those few words only to, say, green.

Is there an easy way to do this in emacs?

Thank you.

like image 788
Dervin Thunk Avatar asked Jan 30 '10 21:01

Dervin Thunk


3 Answers

This is what I've done, using font-lock-add-keywords. I wanted to highlight the words TODO:, HACK:, and FIXME: in my code.

(defface todo-face
'((t ()))
"Face for highlighting comments like TODO: and HACK:")

(set-face-background 'todo-face cyan-name)

;; Add keywords we want highlighted
(defun add-todo-to-current-mode ()
    (font-lock-add-keywords nil
       '(("\\(TODO\\|HACK\\|FIXME\\):" 1 'todo-face prepend))
       t))
like image 179
Marc Avatar answered Nov 11 '22 08:11

Marc


Use library HighLight. You can use overlays or text properties. You can save the highlighting permanently or let it be temporary. You can highlight in many ways (regexp, mouse-drag,...). Lots of possibilities.

like image 45
Drew Avatar answered Nov 11 '22 08:11

Drew


The highlight package has hlt-highlight-regexp-region and hlt-highlight-regexp-to-end, which do exactly what you want.

http://www.emacswiki.org/cgi-bin/wiki/highlight.el

like image 3
charlie Avatar answered Nov 11 '22 07:11

charlie