Colorize text fragments in emacs

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

Is there an easy way to do this in emacs?

Thanks.

+4
source share
6 answers

This is what I did 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)) 
+3
source

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

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

+2
source

Use the HighLight library. You can use overlay or text properties. You can keep the backlight permanently or let it be temporary. You can select in different ways (regexp, mouse-drag, ...). Lots of features.

+2
source

Use the font-lock-add-keywords function to determine a new match for the line in question, linking it to a specific person that you have defined which will appear as green. For instance:

 (font-lock-add-keywords nil '("\\<foo\\>" 0 my-green-face)) 

Please note that you can specify the specific mode in which I wrote nil above, and the corresponding forms can take any of six different styles. See the documentation for the font-lock-keywords variable for rules and a few examples.

+1
source

If you want them to be highlighted only temporarily, I find the Mx highlight-regexp command very useful, especially nice to look at the log files. For example, you created a logging class that displays some trace information like MyClass::function() > when the function starts and MyClass::function() < when it exits (it can be especially useful sometimes when debugging multithreaded problems), then you just ask emacs to highlight some of them green and others red, and then you can see how the execution went.

+1
source

I use what Demetrius suggested. In particular, I have the following two lines in my .emacs

 (global-hi-lock-mode t) (global-set-key (kbd "CMh") 'highlight-regexp) 

Every time I need to highlight a specific word (or regular expression) in the buffer, I press "CMh", which then calls me a word (or regular expression), which I want to show in different ways, and then display it on the face.

0
source

Source: https://habr.com/ru/post/1299841/


All Articles