How to highlight all occurrences of a word in an Emacs buffer?

Headcrab picture Headcrab · Dec 22, 2008 · Viewed 41.1k times · Source

Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?

It doesn't necessarily have to work exactly like Notepad++ (i.e., via selection); ideally, I would like to set up a key binding that causes all occurrences of the word under cursor to be highlighted.

It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.

Also, it would be useful if there was a solution that made it possible to navigate between highlights (using custom key bindings).

Answer

jrockway picture jrockway · Jan 9, 2009

The hi-lock suggestions are good. I think it's easier to use the M-x versions, though:

M-x highlight-regexp RET <REGEXP>

M-x highlight-phrase RET <REGEXP>

highlight-phrase is just a bit of sugar around highlight-regexp that ignores case and translates a space in the regex to match arbitrary whitespace. Handy.