1

I know by M-x set-background-color or M-x set-foreground-color one can change the background/foreground of the the entire buffer. Of course this color change only persists in the current session.

In the same way how can one change the background/foreground color of some characters/words?

For example if in a buffer I have something like below

enter image description here

I would like to change the background/foreground color of some characters like below:

enter image description here

And I would like that this color change to be persistent in the current session unless I delete these colorful character.

2

It's not very clear what you are asking, IMO.

There are various ways to highlight a particular character (e.g. everywhere in the region or in a buffer). Library highlight-chars.el provides one approach.

Likewise, there are various ways to highlight a word (e.g. everywhere in the region or in a buffer). Library Highlight provides one approach. Ordinary font-lock and hi-lock provides another.

Are you also asking about persistence of such highlighting past the current session? If so, that too is possible.

  • I added some explanations. – Name Aug 5 '15 at 19:58
  • I tested highlight-chars.el, it highlights all occurrences of a character. Thus this does not provide the feature I have asked, but it is very useful. Thank you for introducing it. – Name Aug 5 '15 at 20:12
  • 2
    I have tested your second link emacswiki.org/emacs/HighlightLibrary. By M-x hlt-highlight-region I can highlight a region (this region can be a character, a word, etc). The background color is fixed (green). For background color, this partially answer the question. But for foreground color the question still remains. Also if I would like to use different colors, the question still remains. – Name Aug 5 '15 at 20:23
  • Read the commentary of the library and the doc strings of the things you use. See, for example, command hlt-choose-default-face, mentioned in the doc string of command hlt-highlight-region. And read about Emacs faces in the Emacs manual. – Drew Aug 6 '15 at 0:29
  • It turns out that the package highlight is exactly what I was looking for. I am trying to use it and to be familiar with it. By hlt-choose-default-face one can actually use different colors for highlighting (like the second image in my original post). – Name Aug 6 '15 at 11:07
2

This demonstration defines a highlighting color and commands which use anonymous faces to highlight a character or word.

(defvar my-hl-color "#ff00ff")

(defun my-hl-thing (boundaries)
  (put-text-property (car boundaries) (cdr boundaries)
                     'font-lock-face `(:background ,my-hl-color)))

(defun my-hl-character ()
  (interactive)
  (my-hl-thing (cons (point) (1+ (point)))))

(defun my-hl-word ()
  (interactive)
  (my-hl-thing (bounds-of-thing-at-point 'word)))
  • Thank you. my-hl-character seems to work, but let me report that when the character is in a comment area it seems to not working. Let me also report that when a word is the last word of buffer it seems that my-hl-word seems to not properly work. – Name Aug 5 '15 at 21:33

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.