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.

  • 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 Feb 7 '17 at 20: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.

| improve this answer | |
  • 1
    secondary selection is really cool. didn't know about it before this – bernie Feb 8 '17 at 17:17
  • In emacs 25.3.1, the command is C-x X down-mouse-2 instead. – Troy Daniels Jan 14 '19 at 22:56
  • @TroyDaniels: Thanks; typo fixed. – Drew Jan 15 '19 at 5:04

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.


| improve this answer | |
  • 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 Feb 7 '17 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? – Troy Daniels Jan 14 '19 at 22:58
  • That doesn't seem standard. C-M-s has always been regexp search for me. On Mac and Linux. – bernie Jan 14 '19 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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