The answer is yes and no.
If you want highlighting that is automatically re-created in a regular way then you typically want to just use font-lock-keywords
(font-lock-defaults
, font-lock-add-keywords
) for the mode you're interested in.
Or you can put code that does the highlighting using highlight.el
in your init file (or some other file you load), to do the highlighting.
Apart from enriched-mode
, highlighting is not saved with your file. Something - some code - needs to reestablish the highlighting after you reopen your file.
Anything you do interactively to highlight something is actually done by code. You can rerun such code in another session or after reopening a file. You just need to have created the code to use.
See this section of the highlight.el
doc: Temporary or Permanent Highlighting.
To save highlighting permanently, do the following:
M-x enriched-mode
, to put your file buffer in minor mode ‘enriched-mode’. You see ‘Enriched’ in the mode line.
Choose text-property highlighting, not overlay highlighting, by setting option hlt-use-overlays-flag
to nil
. To do this using Customize, choose menu item Highlight using text properties, not overlays.
Choose the highlight face to use: M-x hlt-choose-default-face
.
Highlight in any way provided by library highlight.el
. For example, use hlt-highlighter
to drag-highlight as if using a marker pen.
Save your file. Note that, although highlighting in enriched-text mode modifies the buffer, it does not appear modified to Emacs (check the beginning of the mode line), so if you make no other changes then using C-x C-s
does not save your highlighting changes. To remedy this, just do something besides highlighting — e.g., add a space and delete it — so that C-x C-s
saves to disk.
When you reopen your file later, it is automatically in enriched mode, and your highlighting shows. However, be aware that font-locking can interfere with enriched mode, so you might want to use it on files where you don’t use font-locking.