3

I am trying to highlight particular chars for use in C++ in emacs. I have found the package highlight-chars works very easily. I can use hc-highlight-chars command, it then prompts me for a set of chars and then asks for a face.

However, it expires when I restart emacs. I am very new to emacs so I can't figure out a way to achieve this permanently. Maybe some command I can put in .emacs?

3

You can use the hl-anything package (also available on Melpa) to get persistent highlights.

From its github page,

Once hl-highlight-mode is on, it will save highlights automatically before Emacs exits. It will also try to restore highlights when Emacs opens.

M-x hl-save-highlights to save highlights; M-x hl-restore-highlights to restore highlights.

Saved file-path is stored in hl-highlight-save-file variable.

Check out the rest of the README for more details, including gifs and screenshots.

2

First, just what do you mean by "permanently"? There are two approaches to making highlighting permanent (persistent), depending on what you mean/need.

  1. Whenever the given file is opened, even in a new session, make Emacs rehighlight as it did before. For this, you need to apply the same settings (e.g. use the same commands) whenever that file is opened. You can do this by defining a mode (or even a file handler) that does all the character the highlighting you want, and then associate that mode with the file.

  2. A little-used Emacs feature, which does not always play well with some other features, is Enriched mode. It allows you to highlight text and save the highlighting as part of the text.

    However, enriched mode does not work at all with font-lock highlighting. Font-lock interferes with it. And library highlight-chars.el uses font-lock to effect its highlighting. So it is not possible to use enriched mode to save its highlighting.

    (You can use enriched mode with highlighting that you create using library highlight.el, but not with highlighting that you create using library highlight-chars.el.)

So the answer is #1: set things up so that whenever that particular file is opened (e.g. in a future session) the character highlighting that you need is automatically performed again.

2
  • Since the user is "very new to emacs", it might be nice to point them towards find-file-hook or some manual page.
    – Malabarba
    Jun 25 '15 at 14:52
  • @Malabarba: There are several ways to automatically do something to a particular file. Sure, find-file-hook is one such tool, but it applies to all files, which means that you would be testing, in the hook function, for the particular file(s). There are too many possibilities to list here, and to really be specifically helpful would require knowing just what highlighting commands to apply etc.
    – Drew
    Jun 25 '15 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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