How do I highlight a region and it make it remain highlighted when point moves on.
Why would I want that? So I can make important snippets of code stand out when I review it later.
Library Highlight (highlight.el
) lets you highlight any number of regions in various ways, including using regexps, using any number of faces.
One easy way to highlight an expanse of text (and have it persist until you remove it) is to sweep your mouse over it - like using a highlighter pen. This is command hlt-highlighter
. By default, the command is bound to C-x down-mouse-2
, which means that you use it by hitting C-x X
and then pressing and dragging mouse-2
(the second or middle mouse button) across the text. With a prefix argument (i.e., C-u C-x X
+ drag mouse-2
) you are prompted for the face to use.
(By default, C-x X
is the prefix key that keymap hlt-map
is bound to. Lots of highlighting and highlight-erasing keys are bound in that map. You can put the keymap on any other prefix key or bind individual highlighting and erasing commands to more convenient keys. I put hlt-highlighter
on C-x down-mouse-2
, for example.)
A quick and easy way to highlight a single region of text, and have that highlighting persist until you put it on a different region, is to use the secondary selection.
For that, just press and hold the Meta key (typically Alt
) while you drag mouse-1
.
If you use library second-sel.el
then you can access any number of such secondary selections on a ring, among other features.
C-x X down-mouse-2
instead.
Commented
Jan 14, 2019 at 22:56
Are you looking for hi-lock-mode
? You can highlight matching regexps or highlight regions by using highlight-phrase: C-x w p phrase RET face RET
.
https://www.gnu.org/software/emacs/manual/html_node/emacs/Highlight-Interactively.html
M-s h
prefix, so you can use M-s h C-h
to see a list of related commands. Also worth noting you can save the current set of highlighted patterns to the current file, so that you can restore them later.
M-s
on my emacs (25.3.1 on a Mac) starts a regular expression isearch. I thought that was a standard binding. And I mission something?
Commented
Jan 14, 2019 at 22:58
The code below defines the function highlight-region
that highlights the current region. By default it cycles through the colours in colour-ring
(also defined there) but when called with C-u M-x highlight-region
it prompts for which colour to use instead. I use it to distinguish at a glance some parts of a text file when I jump around in it. It leans more towards simplicity than robustness.
The function unhighlight-regions
removes all highlights created with highlight-region
.
(let ((colours '("Pink" "Burlywood1" "LightGoldenrod1" "#bbf6bb" "#c2eeff" "#d5d1ff" "#fac2ff")))
(defvar colour-ring (make-ring (length colours))
"Colours that ‘highlight-region’ cycles through.")
(dolist (elem colours) (ring-insert colour-ring elem)))
(defun highlight-region (arg)
"Change the background colour of the text in the region
using the first colour in ‘colour-ring’.
With prefix argument, prompt for the colour to use instead.
Rotate ‘colour-ring’ unless a colour different from the
default one is specified."
(interactive "P")
(let* ((default (ring-ref colour-ring -1))
(colour (if arg
(minibuffer-with-setup-hook ; Put region around the initial input.
(lambda () (set-mark (minibuffer-prompt-end)))
(completing-read
"Highlight using colour: "
(defined-colors) nil nil default 'minibuffer-history))
default))
(overlay (make-overlay (region-beginning) (region-end))))
(when (equal (color-values colour) (color-values default))
(ring-insert colour-ring colour))
(overlay-put overlay 'face `(:background ,colour))
(overlay-put overlay 'my-region-highlight t)
(deactivate-mark)))
(defun unhighlight-regions ()
"Remove all highlights made with ‘highlight-region’."
(interactive)
(remove-overlays nil nil 'my-region-highlight t))
bm-toggle
to create a bookmark at the point.