I would like to highlight lines with a certain pattern whenever the file is opened.

Specifically when Journal.org is opened I want to run commands like M-s h l to highlight a set of patterns and assign them background colors.

What is the simplest way to go about doing this (without writing a lot of custom elisp code)?

2 Answers 2


The hi-lock package has support for saving patterns to a file and re-applying them.

  1. Use the standard hi-lock commands to highlight what you are interested in. For example, use M-s h l to highlight lines matching some pattern.
  2. Use M-s h w (hi-lock-write-interactive-patterns) to save the current highlight patterns to the file as a special comment line.
  3. Enable hi-lock-mode or global-hi-lock-mode. For example, if you're only using highlighting in org files you could enable hi-lock mode in an org-mode hook.

The next time you visit that file and hi-lock is enabled, you will be prompted whether you want to highlight the found patterns. You can customize hi-lock-file-patterns-policy if you don't want to be prompted.

  • This is a better solution as it involves less user elisp configuration. I also added (add-hook 'org-mode-hook 'hi-lock-mode) so I don't have to manually enable that mode (or globally toggle it for all files). Aug 27, 2015 at 15:28

This is a classic case where elisp can help you:

(defun my-highlight ()
  (if (string-equal "Journal.org"
                    (file-name-nondirectory buffer-file-name))
      (highlight-lines-matching-regexp "foo" 'hi-yellow)))

(add-hook 'find-file-hook #'my-highlight)
  • Thanks! Next I will have to auto-open the last two items in org hierarchy, and I'll be all set... Aug 25, 2015 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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