How to highlight all occurrences of a word in an Emacs buffer?
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).
Solution 1:
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.
Solution 2:
Maybe highlight-symbol.el at http://nschum.de/src/emacs/highlight-symbol/ is what you are looking for:
Solution 3:
Type C-s
, then type the current word or type C-w
. As a bonus, you can now hit C-s
again to search for the word.
This is called incremental search.
Solution 4:
What I use is idle-highlight
http://www.emacswiki.org/emacs/IdleHighlight
M-x idle-highlight sets an idle timer that highlights all occurences in the buffer of the word under the point.
To enable it for all programming modes, in ~/.emacs.d/init.el
:
;; highlight words
(add-hook 'prog-mode-hook (lambda () (idle-highlight-mode t)))