Highlighting Regular Expressions

Charles Choi has another Emacs nugget: how to (persistently) highlight isearch targets. The idea is that you may have a lot of hits for your search and would like to keep them highlighted so that you can examine them in detail. That turns out to be pretty simple. You simply type Meta+s h r and Emacs will exit isearch mode and highlight the text matching the regular expression. You can also highlight the entire line containing the match by typing Meta+s h l.

His post stirred a (very) distant memory so I checked Irreal and discovered that this subject is like the Phoenix: it keeps rising from the ashes. I’ve written about it at least twice, the first time in 2011, and the last time in 2022. If you check those posts, you’ll discover that there are several related commands that highlight objects in similar ways.

Choi’s post is an update to his previous post on using transient for isearch that I also wrote about. He says that although he loves the highlighting commands, he’d never be able to remember them so he added them to his transient menu.

Every time I write about this, I’m firmly convinced that the bindings make perfect sense and I’ll be able to remember them. Alas, Choi is correct. I can’t even remember that the commands exist let alone what their bindings are.

As a final point, it’s worth noting that the commands are actually a bit more general than Choi states. The isearch-highlight-regexp command actually calls highlight-regexp and passes it the current isearch target regexp and highlighting color. Similarly for the whole line variant. Thus you can highlight objects in the buffer without going through isearch. Happily, the same keybindings work in either case so if you can remember them it’s the same whether or not you’re coming from isearch.

This entry was posted in General and tagged . Bookmark the permalink.