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.