2022-06-13 Highlighting and de-highlighting things

From time to time I want to perform a kind-of “human search” on a file. For example, there is some keyword – or maybe something describable by a regex – and I want to be able to easily spot all of its occurrences in some file. For some reason, isearch or Swiper won’t work for me. (This may happen if, for instance, I don’t exactly know what I’m looking for. Imagine going through some file and deciding to search for some keywords only when I actually see them for the first time. So, I’m skimming a buffer and once I see the word “banana”, I go “hey, bananas are good, let’s find more of them” and then I want to easily see every line containing the word “banana”. Skimming further, I can see the word “apple” and I suddenly have a craving for apples, so I want to add apples to things that should be easily seen. And so on.)

It turns out that Emacs has a few commands which can help with that. One is highlight-regexp (it should be fairly obvious what it does; by the way, I mentioned it a few years ago); another one is highlight-lines-matching-regexp (also pretty self-explanatory). A slightly less obvious one is highlight-phrase (it is similar to highlight-regexp, but it interprets a space as “match any number of spaces or tabs”). Yet another one is highlight-symbol-at-point. You can also use unhighlight-regexp to cancel the highlighting. (A nice touch is that if you use the highlighting commands more than once, unhighlight-regexp allows you to choose which one to cancel. Also, you can see them all – and a few others – if you press M-s h C-h, since they are bound to a keymap bound to M-s h.)

One thing that occurred to me a few days ago is that these commands can be used to achieve the opposite effect, too – in essence, you can “unhighlight” (is “lowlight” a word?) a regex, a line containing one etc. The trick is that most of the highlighting commands (for some reason, highlight-symbol-at-point is an exception – you have to use a prefix argument to change that) allow you to choose the color used for the highlighting (in Emacs parlance, the face). You see, one of the faces defined by default in Emacs is called shadow, and it means just gray foreground and unchanged background. This means that “highlighting” stuff with that particular face makes it “dimmed”; also, since the highlighting commands employ the font-lock mechanism in a way that any highlighting overrides the standard font-lock, any syntax-related fontification is gone once you use the shadow face with the highlighting commands. (I wish I knew how it is done. I thought it uses font-lock-keywords, but apparently it does not.)

Anyway, the highlighting commands are a great way to temporarily make some text stand out – or fade out, so to speak. Definitely not something you would use every day, but can be very handy once in a while.

CategoryEnglish, CategoryBlog, CategoryEmacs