30

highlight-symbol-at-point is really useful however I could not find an easy way to un-highlight symbols.

Currently I have to call unhighlight-regexp repeatedly for each highlighted symbol.

Is there a better way to:

  • unhighlight all highlighted symbols in a buffer
  • unhighlight a symbol at point?
kostya
  • 503
  • 4
  • 6

4 Answers4

21

Is there a better way to unhighlight all highlighted symbols in a buffer?

From C-h f unhighlight-regexp, we get

(unhighlight-regexp REGEXP)

Remove highlighting of each match to REGEXP set by hi-lock. Interactively, prompt for REGEXP, accepting only regexps previously inserted by hi-lock interactive functions. If REGEXP is t (or if C-u was specified interactively), then remove all hi-lock highlighting.

That means that we simply need to call unhighlight-regexp with t as its argument to remove all hi-lock highlighting in the buffer.

If it were a one-time thing, it could be quickly done by M-: (unhighlight-regexp t) or C-u M-s h u. But it seems that this is something you need to do quite often. In that case, it's better to wrap it in an interactive function and bind it to some key.

Below is a proposed solution where my/unhighlight-all-in-buffer is the wrapper interactive function and it is bound to hU in the search-map. By default the search-map prefix is M-s. So now, just as M-s h u launches unhighlight-regexp, M-s h U (note the capital U) launches my/unhighlight-all-in-buffer.

(require 'hi-lock)        
(defun my/unhighlight-all-in-buffer ()
  "Remove all highlights made by `hi-lock' from the current buffer.
The same result can also be be achieved by \\[universal-argument] \\[unhighlight-regexp]."
  (interactive)
  (unhighlight-regexp t))
(define-key search-map "hU" #'my/unhighlight-all-in-buffer)

Is there a better way to unhighlight a symbol at point?

The good thing is that you do not need to place the point on a symbol you need to unhighlight.

You simply call M-x unhighlight-regexp or M-s h u (its default binding) and hit M-p/M-n till you come across the regexp of the symbol you need to unhighlight, and hit Enter.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
10

C-u C-x w r

(unhighlight-regexp REGEXP) (...) If REGEXP is t (or if C-u was specified interactively), then remove all hi-lock highlighting.

rofrol
  • 186
  • 2
  • 8
4

Another way is to use library Highlight (highlight.el), commands hlt-highlight-symbol-at-point and hlt-unhighlight-symbol-at-point.

They are bound default to C-x X h s and C-x X u s, respectively. All of the highlighting and unhighlighting commands are bound on prefix key C-x X by default. Highlighting commands use prefix key C-x X h. Unhighlighting commands use prefix key C-x X u. You can use a different prefix key than C-x X by just binding it to prefix command hlt-map.

Highlighting in library Highlight can use overlays or text properties (or both). The highlighting face or color can be chosen automatically (from a customized sequence of faces and colors) or not.

Highlighting can use property font-lock-face, so that it is controlled by Font Lock mode. Or it can use property face, so that it is not controlled by it. (You can use property face to highlight in buffers that do not use font-lock, and the highlighting remains in font-locked buffers when you turn off Font Lock mode.)

Drew
  • 75,699
  • 9
  • 109
  • 225
1

This will unhighlight a single symbol:

(defun unhighlight-symbol-at-point ()
  "Remove highlight of symbol at point."
  (interactive)
  (unhighlight-regexp (concat "\\_<" (thing-at-point 'symbol) "\\_>")))
Mark
  • 828
  • 10
  • 15