This is BrainLog, a blog by Dan Sanderson. Older entries, from October 1999 through September 2010, are preserved for posterity, but are no longer maintained. See the front page and newer entries.

April 19, 2007

Newbie Emacs tip: One way to set the background and foreground colors for an Emacs window is with the set-background-color and set-foreground-color commands:

(set-background-color "#003344")
(set-foreground-color "white")

These colors are my favorites, and look great on Emacs running in its own window. Unfortunately, if I run Emacs in a terminal window with this configuration, the nice dark green background gets rendered as ugly bright green. So I use conditional logic in my .emacs file to only set these colors when running in its own window:

(when window-system
  (set-background-color "#003344")
  (set-foreground-color "white"))

However, set-background-color does not set the default color to be used by new windows (new "frames" in Emacs jargon), such as one opened when you type M-x 5 2. This does not appear to be true for functions that adjust faces directly, such as with set-face-foreground, or with Emacs's Customize feature. One way to set the background color for default frames is as follows:

(add-to-list 'default-frame-alist '(background-color . "#003344"))

With this in your .emacs file, the explicit set-background-color et al. do not appear to be necessary. So:

(when window-system
  (add-to-list 'default-frame-alist '(background-color . "#003344"))
  (add-to-list 'default-frame-alist '(foreground-color . "white")))

From this, it appears set-background-color et al are not meant to be used to set defaults, only change the current frame's settings. Good to know.

P.S. I also use (set-cursor-color "red"), or rather, (add-to-list 'default-frame-alist '(cursor-color . "red")), on the dark green background.

comments...

They making you use emacs at Google, then? :-)

Heh, that's a funny thought. Nobody makes anybody do anything at Google. It's really weird.

(And of course, I've been Emacs-ing for years, but you knew that. :) )

This entry is no longer accepting comments. Feel free to contact me if you have a question or a correction. Thanks!