32

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?
5
  • Do you need to unhighlight all symbols in one go? Commented Jan 27, 2016 at 4:04
  • Most of the time - yes. Occasionally unhighlighting a symbol at point is useful as well.
    – kostya
    Commented Jan 27, 2016 at 4:06
  • The answers would be different if you want to unhighlight the symbol under point or unhighlight all symbols. Commented Jan 27, 2016 at 4:08
  • I've updated my question
    – kostya
    Commented Jan 27, 2016 at 4:17
  • 3
    You can remove all highlighted symbols using M-x hi-lock-mode Commented Jan 18, 2017 at 15:50

4 Answers 4

24

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.

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.

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

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) "\\_>")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.