Emacs: Run Elisp Code When File Opens (Persistent Highlight)

By Xah Lee. Date: . Last updated: .

You can set a file to automatically eval elisp code when the file opens.

Here's a example. When opening a file, some words or lines are automatically colored.

add this to the end of file:

/* Local Variables: */
/* eval: (highlight-phrase "\\bJane\\b" (quote hi-pink)) */
/* eval: (highlight-phrase "\\bJohn\\b" (quote hi-yellow)) */
/* eval: (highlight-phrase "\\bDavid\\b" (quote hi-green)) */
/* eval: (highlight-phrase "\\bKing\\b" (quote hi-blue)) */
/* eval: (highlight-lines-matching-regexp "^#" (quote hi-blue-b)) */
/* eval: (highlight-phrase "\\bQueen\\b" (quote hi-red-b)) */
/* End: */

Note: this block of code must be near the end of file.

The /* … */ is optional. It is C's block comment. You can use any other language's comment.

When file is opened that has embeded local elisp code, , emacs will usually prompt warning, asking if you would allow the code to evaluate.

Emacs Persistent Highlight