88

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).

nbro
  • 15,395
  • 32
  • 113
  • 196
Headcrab
  • 6,838
  • 8
  • 40
  • 45

13 Answers13

96

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.

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
jrockway
  • 42,082
  • 9
  • 61
  • 86
  • 26
    To clear the highlights, use: `M-x unhighligh-regex` key-chord: `C-x w r` ... Also, *highlight-phrase* is not fully case insensitive. Only **initial** lower-case letters (of words) ae made case insensitive. (I suppose it was intended to deal with `fred` vs `Fred`) .. eg it generates a regex like: `[Cc]at[ ]+[Dd]og` – Peter.O Aug 19 '11 at 06:30
  • Thanks, I find your comment about the difference clearer than the built-in help. – Joshua Goldberg Feb 17 '14 at 16:25
  • 1
    Note: Typo: It should be `unhighlight-regexp` instead of `unhighligh-regex` (missing`t` and `p`). – Zelphir Kaltstahl Feb 19 '17 at 12:06
  • It's very helpful. I have 2 questions. 1. Is there a way to highlight 2 words at the same time with different colors? Something like `M-x highlight-regexp RET , `. 2. How to `unhighlight` all words at the same time? Something like `M-x unhighlight-regexp RET , ` – Just a little noob Feb 25 '22 at 07:53
42

Maybe highlight-symbol.el at http://nschum.de/src/emacs/highlight-symbol/ is what you are looking for:

alt text

Community
  • 1
  • 1
polyglot
  • 9,945
  • 10
  • 49
  • 63
  • 1
    I tried it but couldn't get it working - emacs complained about "Symbol's value as variable is void: highlight-symbol-idle-delay". – Headcrab Dec 24 '08 at 02:20
  • 1
    I did have that particular problem. I ended up commenting out the entire (defcustom highlight-symbol-idle-delay...) part, and move (defvar highlight-symbol-idle-delay...) before (defun highlight-symbol-update-timer...), which will avoid the error. – polyglot Dec 26 '08 at 03:34
  • 1
    Yes, finally I did the same exact thing. – Headcrab Dec 26 '08 at 08:45
  • 1
    Sorry, but I don't see the necessity to add an external package for this task! See jrockway's post. – mefiX Nov 11 '10 at 09:36
  • 5
    @mefiX: the build-in function is nice for single uses. The external package add the convenience of (among others) highlighting the word under cursor with just 1 keystroke, as well as automatically change highlighting color for different words. Yes you can do that with the build-in function, but why waste minutes and hundreds of keystroke instead of just a few seconds and keystrokes, if that's something you do frequently? Whoever downvotes seems narrow-sighted: someone might find this package useful, even if you don't. – polyglot Dec 06 '10 at 08:29
  • 1
    @zengr It's working for me now, and it's the exact amount of kill I want. – Shon Mar 23 '15 at 08:00
  • This seems very nice, but how does it know whether it has found the same symbol or another symbol with the same name? Does it do a complete parsing of all code in the project? – HelloGoodbye Jun 26 '15 at 01:51
  • 1
    Note: this package is buggy and unmaintained, and [needs to be deprecated in preference of symbol-overlay mode](https://github.com/nschum/highlight-symbol.el/pull/53), which provides same functional, but is faster and better. – Hi-Angel Apr 22 '19 at 23:33
37

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
ShreevatsaR
  • 38,402
  • 17
  • 102
  • 126
  • 11
    For what it's worth, I now realize the intent of the question: you want the occurrences highlighted "permanently" (until you turn it off) even as you do other work like moving around, typing, etc. For this, C-s (incremental search) will not work (the highlighting is "temporary"; you can move about in the buffer but it's hard to do new editing work). I use (and have upvoted) the `M-x highlight-regexp` solution (and bound it to a shortcut). – ShreevatsaR Oct 25 '12 at 05:21
16

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)))
anol
  • 8,264
  • 3
  • 34
  • 78
Johnny
  • 1,144
  • 2
  • 11
  • 23
10

Light-symbol will highlight whatever symbol point is over.

Alternately, you can use occur, which lists all lines matching a regexp. It's useful to quickly see all functions in a class.

ieure
  • 2,381
  • 1
  • 16
  • 16
  • This seems very nice, but how does it know whether it has found the same symbol or another symbol with the same name? – HelloGoodbye Jun 26 '15 at 01:56
7

Nobody mentioned symbol-overlay mode. It's basically a better rewrite of highlight-symbol-mode. "Better" as in, lacks bugs of original highlight-symbol (such as, temporary highlight getting stuck, or the temporary highlight disappearing for moving inside the highlighted word; or not being able to highlight symbols like *), better integrated, and maintained. See "Advantages" paragraph of its README.

You can install it as usual, with M-xpackage-install (make sure to update package list beforehand with package-list-packages). For reference, at the bottom I attached code I use to enable the mode and disable a few of the more advanced features which you may or may not want.

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?

Once you enable overlay-symbol, occurrences on the screen will be shown for every word that you put cursor upon after a timeout (timeout by default is 0.5s, can be configured with symbol-overlay-idle-time variable). If a word don't get highlighted, this means there's just one match on the screen (the one you put cursor upon), hence there's no need to highlight it.

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.

To highlight the word under cursor permanently there's a function symbol-overlay-put. To unhighlight call it once again.

In my config example it's bound to Logo+` key.


(require 'symbol-overlay)
(defun enable-symbol-overlay-mode ()
  (unless (or (minibufferp)
              (derived-mode-p 'magit-mode)
              (derived-mode-p 'xref--xref-buffer-mode))
    (symbol-overlay-mode t)))
(define-global-minor-mode global-symbol-overlay-mode ;; name of the new global mode
  symbol-overlay-mode                                ;; name of the minor mode
  enable-symbol-overlay-mode)
(global-symbol-overlay-mode)                         ;; enable it
(global-set-key (kbd "s-`") 'symbol-overlay-put)
(setq symbol-overlay-ignore-functions nil)           ;; don't ignore keywords in various languages
(setq symbol-overlay-map (make-sparse-keymap))       ;; disable special cmds on overlays
Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
  • `symbol-overlay-mode` was exactly what I was looking for. Binding `symbol-overlay-put` to an F key is handy for toggling highlight of a given word across the file. – Matt Kneiser Sep 03 '19 at 18:12
  • Can ```symbol-overlay-put``` be case insensitive? – efe373 Jan 18 '22 at 04:58
  • 1
    @efe373 I'm afraid I don't see such possibility. I tried modifying `case-fold-search` but it has no effect on the text highlighted by `symbol-overlay-put`. I think you could create a feature request [on the project bugtracker](https://github.com/wolray/symbol-overlay/issues). – Hi-Angel Jan 18 '22 at 11:42
6

Try http://www.emacswiki.org/emacs/msearch.el All occurences of the text selected with the cursor are highlighted. You have to drag over the string which you want to highlight. That enables you to easily change the selection without changing the highlight.

If you want to preserve the highlighting of a string you can freeze it.

You can enslave a buffer to another buffer. Text selected in the master buffer will also be highlighted in the slave buffer. That is useful for comparing buffers. It is also useful for taking notes in one buffer while you investigate the text in another one. You can have a collection of keywords in the notes buffer. Drag over such a keyword and its occurences in the investigated text will be highlighted.

I am using this stuff for years now. I added the freezing quite recently. So, maybe something is broken. If this is the case leave me a note on http://www.emacswiki.org/emacs/msearch or here.

Tobias
  • 5,038
  • 1
  • 18
  • 39
6

This may not be as nice as what you were hoping but if you put

(global-hi-lock-mode 1)

in your .emacs file then you can type C-x w h REGEX <RET> <RET> to highlight all occurances of REGEX, and C-x w r REGEX <RET> to unhighlight them again. Again, not as elegant as you'd probably like, but it'll work.

dancavallaro
  • 13,109
  • 8
  • 37
  • 33
5

Check Interactive Highlighting

Should be:

C-x w h word <RET> <RET>

nbro
  • 15,395
  • 32
  • 113
  • 196
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 2
    First, hi-lock-mode should be enabled by M-x hi-lock-mode. Then, it works. Btw the link to interactive highlighting is dead. It is here now: https://www.gnu.org/software/emacs/manual/html_node/emacs/Highlight-Interactively.html – aspirin Dec 13 '13 at 14:54
3

Try iedit. It highlights the word at point and lets you edit all occurrences of it easily. With an additional keystroke (C-'), it hides all the lines without that word in it. Very handy!

Ehvince
  • 17,274
  • 7
  • 58
  • 79
Phob
  • 891
  • 8
  • 13
2

Commands in library highlight.el let you (un)highlight text matching a regexp (in this case a symbol), using overlays or text properties. You can cycle among the occurrences. Highlighting can be temporary or persistent. (more info).

Drew
  • 29,895
  • 7
  • 74
  • 104
0

This maybe won't highlight but will search for a word without you needing to type it...

when you've reached the word you wanted to search, C-S, then read the full word with C-W then you can C-S and it will search for it. In my Emacs it also highlights all instances in the document.

George
  • 137
  • 1
  • 17
0

This package available in Melpa works, you can customize the highlight style as well.

https://github.com/ignacy/idle-highlight-in-visible-buffers-mode

Pharaoh
  • 712
  • 1
  • 9
  • 33