13

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.

1
  • Bookmarks are probably what you want to check into. I don't know how to bookmark a region, but I regularly use bm-toggle to create a bookmark at the point.
    – ebpa
    Commented Feb 7, 2017 at 20:11

3 Answers 3

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

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

4
  • 1
    secondary selection is really cool. didn't know about it before this
    – bernie
    Commented Feb 8, 2017 at 17:17
  • In emacs 25.3.1, the command is C-x X down-mouse-2 instead. Commented Jan 14, 2019 at 22:56
  • @TroyDaniels: Thanks; typo fixed.
    – Drew
    Commented Jan 15, 2019 at 5:04
  • Someone care to explain the downvote (5 years later)?
    – Drew
    Commented May 28 at 14:20
7

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

3
  • 1
    +1, I use these commands a lot. By default the hi-lock commands are bound with a 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.
    – glucas
    Commented Feb 7, 2017 at 21:14
  • 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
  • That doesn't seem standard. C-M-s has always been regexp search for me. On Mac and Linux.
    – bernie
    Commented Jan 14, 2019 at 23:35
0

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

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.