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:
| Chord | Function | Description |
|---|---|---|
M-s h r | highlight-regexp | Highlight matches of pattern REGEXP in current buffer with FACE |
M-s h p | highlight-phrase | Highlight matches of phrase PHRASE in current buffer with FACE |
M-s h l | highlight-lines-matching-regexp | Highlight lines containing matches of REGEXP in current buffer with FACE |
M-s h . | highlight-symbol-at-point | Highlight the symbol found near point without prompting |
M-s h u | unhighlight-regexp | Remove highlighting on matches of REGEXP in current buffer |
M-s h w | hi-lock-write-interactive-patterns | Write active REGEXPs into buffer as comments (if possible) |
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:

Let's add a highlight rule for the word Function:
You can cancel the highlighting rules (for each rule separately) by M-s h u.
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:
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-modeBy 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