0

I would like to highlight each occurence of some word e.g. "covid" in a text when the file is loaded.

It will be in org files but even global scope for all files is not a problem. So, I can put the code in .spacemacs config file so it is loaded upon each Spacemacs startup and the words specified are being highlighted.

Here is a rough example of what I am looking for (I used search to highlight covid as an example - in my case I would prefer different background and foreground colors):

enter image description here

So, how does one get to something like that?

I have found this code:

  (add-hook 'org-mode-hook
            (lambda ()
              (font-lock-add-keywords nil
                                      '(("\\<covid\\>" 1
                                         font-lock-warning-face t)))))

But it isn't working.

I would also need to modify the color and background color of the string.

How to do that?

I am not even sure whether (font-lock-add-keywords) is the right function for this job. If there is something better or more apt for this job, let me know.

Also, (font-lock-warning-face), I don't know if I have to use it or I should create some own foreground and background colors in some function that will be replacing font-lock-warning-face?

UPDATE:

I have tried this code:

  (add-hook 'org-mode-hook
            (lambda ()
              (font-lock-add-keywords nil
                                      '(("\\<covid\\>" 1
                                         '(:foreground "white" :background "red") t)))))

and after restarting Emacs I got the result is this:

enter image description here

Funny enough, when I try to edit the headers 1, 2, and 3 - only the 1st and 3rd go back with the style. The 2nd stays screwed no matter what I do. I looks like this:

enter image description here

So, it seems to me that "covid" is detected by Emacs, but for some reason the code doesn't highlight the word with the red background and white color, as I stated in the add-hook code in my .spacemacs file.

Any idea, how to fix that?

1 Answer 1

1

If you want Emacs-wide highlighting do

(setq my-hi-lock-patterns
      '(("covid\\(-19\\)?" (0 'hi-pink t))
        ("corona" (0 'hi-yellow t))))

(add-hook 'find-file-hook
          (defun my-hi-lock ()
            (hi-lock-mode 1)
            (font-lock-add-keywords nil my-hi-lock-patterns t)
            (font-lock-flush)))

Did you look at the reference manual first?

If you did this, you would have discovered hi-lock-mode. And if you did M-x hi-lock-mode, then in the Menu Bar you would have seen

Menu Bar -> Edit → Regexp Highlighting → Highlight Regexp...

Commands for Regexp Highlighting


To get this

Demo of hi-lock-mode

do this

  1. Create somefile.org with following content
* Do do corona pariatur voluptate amet, sed covid

** Dolor aliquip veniam, ut eu elit covid

Et est lorem do anim occaecat nisi sed commodo culpa culpa cillum duis
quis sed dolore et officia.  Duis veniam, lorem pariatur est qui
officia ea.  Laboris ex velit ea do cillum officia voluptate.  Sit in
officia enim corona consectetur occaecat fugiat incididunt nulla ad
tempor laborum qui occaecat eu dolor incididunt do exercitation.
Pariatur excepteur non dolore eu nulla adipiscing officia mollit irure
covid-19 pariatur exercitation pariatur id sit.  Deserunt covid eiusmod
consectetur incididunt ad pariatur duis id cillum eu ut cillum ad
fugiat.  Sint fugiat elit, culpa veniam, tempor anim duis enim.

# Hi-lock: (("corona" (0 (quote hi-yellow) t)))
# Hi-lock: (("covid\\(-19\\)?" (0 (quote hi-pink) t)))
# Hi-lock: end

# Local Variables:
# eval: (hi-lock-mode 1)
# End:

  1. emacs -Q

  2. C-x C-f somefile.org RET

  3. Agree to whatever Emacs ask you.

  4. You will see something like this

Demo of hi-lock-mode

  1. Profit!

For more information, read the manual

  1. C-h r ( Help → Read the Emacs Manual)

  2. I highlight (Info -> Index -> Lookup a string and display index of results...)

  3. You will something like this

Index entries that match ‘highlight’:

\0[index\0]
* Menu:

* cpp-highlight-buffer [Command Index]:  Other C Commands. (line 56)
* highlight-changes-mode [Command Index]: Highlight Interactively. (line 6)
* highlight-lines-matching-regexp [Command Index]: Highlight Interactively. (line 60)
* highlight-phrase [Command Index]:      Highlight Interactively. (line 65)
* highlight-regexp [Command Index]:      Highlight Interactively. (line 28)
* highlight-symbol-at-point [Command Index]: Highlight Interactively. (line 72)
etc etc
  1. Follow the highlight interactively links.
7
  • You don't understand me. I don't want to use a highlight functionality of emacs at all. And I have to have this code in .spacemacs. It has to be global or mode wide - not in local files via eval: . Also, try your solution with the words in the headlines that use backgroudn different from white - just switch your theme to leuven (it's an all Emacses), for example. You will be surprised how it behaves.
    – fegax
    Jul 18 at 10:23
  • Seems like I need to work with this gnu.org/software/emacs/manual/html_node/emacs/Font-Lock.html . I tend to thinking of it as enriching/adding additional syntax highlighting "classes" or properties. So, if there is a special coloring/fontifying of the org-document-title or org-level-1 I need something similar but for my own strings. "covid" is just and example - for simplicity. Later I want to use regex to get more complicated things, e.g. dates of specific format etc. But the regex part - I think I will be able to do it, I just don't know how to get to the regex part ;D
    – fegax
    Jul 18 at 10:35
  • 1
    Add it to find-file-hook. See the top of my updated answer here emacs.stackexchange.com/a/72650/31220. hi-lock-mode does a font-lock-add-keywords internally. Jul 18 at 10:47
  • :DD It works now. How is that possible? Thank you very much. One minor question. How would the code look like if I wanted not show it globally but only for a certain mode e.g. org-mode?
    – fegax
    Jul 18 at 10:54
  • 1
    Change find-file-hook to org-mode-hook. Jul 18 at 10:57

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.