Hi-Lock-Mode

The hi-lock-mode helps to make locally-significant (within the curent buffer) highlighting of regular expressions.

There are several interactive commands for specifying the highlighting rules:

ChordFunctionDescription
M-s h rhighlight-regexpHighlight matches of pattern REGEXP in current buffer with FACE
M-s h phighlight-phraseHighlight matches of phrase PHRASE in current buffer with FACE
M-s h lhighlight-lines-matching-regexpHighlight lines containing matches of REGEXP in current buffer with FACE
M-s h .highlight-symbol-at-pointHighlight the symbol found near point without prompting
M-s h uunhighlight-regexpRemove highlighting on matches of REGEXP in current buffer
M-s h whi-lock-write-interactive-patternsWrite active REGEXPs into buffer as comments (if possible)

Why?

For example, to quickly highlight all lines in the buffer starting with the word "For" you can say M-s h l, "^For" and choose the face highlight (or any other). The result is as follows:

https://gallery.norre.org/images/2020/12/10/emacs-hi-lock-4.png

Let's add a highlight rule for the word Function:

https://gallery.norre.org/images/2020/12/10/emacs-hi-lock-5.png

You can cancel the highlighting rules (for each rule separately) by M-s h u.

Killer-Feature

Highlighting rules can be saved in the current buffer by M-s h w. Our set of regexps will be saved in this way:

;; Hi-lock: (("Function" (0 'highlight prepend)))
;; Hi-lock: (("^.*\\(?:^There\\).*\\(?:$\\)
;; ?" (0 'highlight prepend)))

Now, the important part. Rules, formed like this, are processed by hi-lock-mode and getting applied to the current buffer. This means, that you can save the highlighting rules for each buffer/file separately.

I actively use this mode to maintain my knowledge base. Even though I don't use the zettelkasten method in its entirety, I create a separate org-document for each individual knowledge atom. For short and atomic articles it is very easy to create short set of highlighting rules to make reading easier.

For example, for articles on OSPF, I use the following set of rules hi-lock-mode:

# Hi-lock: (("e?i?BGP " (0 (quote org-verbatim) prepend)))
# Hi-lock: (("OSPF" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("LSDB" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("NSSA" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("OSPFv[23]" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("\sTCP" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("B?DR" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("IPv?[46]?" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("[IB]R" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("AS?BR" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("\sLSA" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("e?i?BGP " (0 (quote org-verbatim) prepend)))
# Hi-lock: (("\sASBR" (0 (quote org-verbatim) prepend)))
# Hi-lock: (("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" (0 (quote org-code) prepend)))

This is how these rules work, using the example of an introductory article on OSPF:

https://gallery.norre.org/images/2020/12/10/emacs-hi-lock-3.png

Configuration

To enable hi-lock-mode globally:

(global-hi-lock-mode)

To enable hi-lock-mode for org-mode only:

(add-hook 'org-mode-hook 'hi-lock-mode t) ;; enable hi-lock-mode for org-mode

By default, if hi-lock-mode finds its configuration in an opened file, hi-lock-mode asks whether to apply this configuration or not. To disable this behavior:

(setq hi-lock-file-patterns-policy (lambda (pattern) t)) ;; don't ask for permission. don't ask for forgiveness