ChatGPT解决这个技术问题 Extra ChatGPT

How do I change the highlight color for selected text with Emacs / deftheme?

I'm using Emacs 24; I've installed the zenburn theme, which is great, except I cannot see the selection highlight easily with the highlight color provided by zenburn:

https://i.stack.imgur.com/pZwnZ.png

By "selection" color, I mean the color of text that I've selected by setting a mark (C-space and moving the cursor to select text).

For the life of me, I cannot figure out how to change it. I've tried changing every combination of highlight, selection, etc.. that I can think of in zenburn-theme.el, but nothing seems to change it.

**For sanity's sake, I've tried changing other colors in the theme to make sure Emacs is loading the file properly - it is - those changes work.*

I would have especially thought that changing highlight would work, but no customizations to the highlight line seem to work:

;;;; Built-in                                                                                                                                                                            
;;;;; basic coloring                                                 
   ...                                                                                                      
   `(highlight ((t (:background ,zenburn-bg-05 :foreground ,zenburn-yellow))))  

How can I change the selection color?


t
tungd

What you're looking for is the region face. For example:

(set-face-attribute 'region nil :background "#666")

That was exactly what I need!
This does not apply for the selected texts on isearch, is there any way to change background color on the searched texts as well?
@alper isearch uses a different face, a quick look at isearch.el reveal that it is quite complicated since isearch seems to be using highlight-regex for highlighting. You'll probably need to dig in a bit further to find the face name that you want.
P
Philippe

In addition to tungd's solution. You could also then change the font color to make it more readable. E.g. to white

(set-face-attribute 'region nil :background "#666" :foreground "#ffffff")

Just as a heads-up to Emacs n00bs like myself, this solution is complete, but it also overwrites any syntax-highlighting you have on the selected region because of the foreground.
M
Micah Elliott

You can use the customize interface.

M-x customize-face

When prompted for which face, enter region. Then you'll see something like:

https://i.stack.imgur.com/L1uM0.png

Using customize makes it easy to experiment with lots of colors. Try clicking [ Choose ] to access the color-picker), then [ Apply and Save ]. It also nicely organizes your customizations into a single file (rather than further polluting your init.el).