2

I have an init file for Emacs located at /home/username/.emacs.d/init.el and it has the command (set-background-color "honeydew").

However, when I open Emacs the color has obviously not changed from black. How can I make the file load on startup?

It does work if I run M-x set-background-color but it isn't persistent.

4
  • do you happen to also have a .emacs file in your HOME path?
    – shoshin
    Jan 28, 2019 at 5:10
  • @GrantShangreaux Could you please tell me how I can check? I have only used Linux for about 1 week.
    – Adam G
    Jan 28, 2019 at 5:11
  • cd will take you to your home directory, then ls -a will show you the contents of the directory, including hidden files.
    – shoshin
    Jan 28, 2019 at 5:12
  • How do you start Emacs? Jan 30, 2019 at 9:52

2 Answers 2

5

set-background-color does work in your init file! It's just that the setting is overridden later in Emacs's startup process, which goes (in part) something like this:

  • load site init files
  • load user init file
  • load packages
  • set up frame parameters

To set up window frame attributes and have them actually persist, you need to set up variables (customization options) that are used in that last step when Emacs configures the UI.

To customize frame settings such as width, height, background color, set the default-frame-alist variable (see the chapter "Frames and Graphical Displays" in the Emacs manual, under the section "Frame Parameters"). Or you can customize it through the menus: Options -> Customize Emacs -> Specific Option -> Initial Frame Alist.

If you need more complex control of the window attributes and how they're created, you'll have to create a custom function and add it to window-setup-hook.

1
  • Thank you! I had a similar issue, and this fixed it. While I love Emacs in general, its outright hostility to backwards compatiblity really grinds me. Any time I install a new version, its a garunteed 4+ hours getting my .emacs to work again, and most of the time I'll still flat out lose stuff that used to work fine. In this case I've had the (set-foreground-color and (set-background-color entries in my .emacs for 25 years. That's a quarter of a century! But as soon as I installed version 27, it doesn't work any longer.
    – T.E.D.
    Apr 19, 2023 at 14:53
3

set-background-color is an interactive compiled Lisp function in `frame.el'.

(set-background-color COLOR-NAME)

Set the background color of the selected frame to COLOR-NAME. When called interactively, prompt for the name of the color to use. To get the frame's current background color, use `frame-parameters'.

I'm going to guess that you're starting Emacs with emacs --daemon, in which case an init file containing (set-background-color "honeydew") is setting the background of a frame you'll never see.

And even if that's not the case, your setting wouldn't be repeated in other frames (e.g. C-x52), so you want to take care of that issue regardless.

(defun my-set-background-color (&optional frame)
  "Set custom background color."
  (with-selected-frame (or frame (selected-frame))
    (set-background-color "honeydew2")))

;; Run later, for client frames...
(add-hook 'after-make-frame-functions 'my-set-background-color)
;; ...and now, for the initial frame.
(my-set-background-color)

I've set honeydew2 here because I honestly can't tell the difference between honeydew and white on my monitor. (That may or may not be a contributing factor for you too?)


Edit: All of that said... an initial terminal frame may still be a problem, and you can both deal with that and replace everything I've written above with just:

(add-to-list 'default-frame-alist '(background-color . "honeydew2"))
1
  • (add-to-list 'default-frame-alist '(background-color . "grey")) is the correct answer here. See also Sue's answer for more info on the why.
    – may
    Sep 4, 2021 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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