Emacs: Toggle Background Color 📜

By Xah Lee. Date: . Last updated: .

Cycle Background Color

(defvar xah-cycle-colors [ "cornsilk" "seashell" "honeydew"] "A vector of color names (string) for `xah-cycle-background-color'. URL `http://xahlee.info/emacs/emacs/emacs_toggle_background_color.html' ")
(defun xah-cycle-background-color ()
  "Cycle background color among values in variable `xah-cycle-colors'.

Return the new color name.

URL `http://xahlee.info/emacs/emacs/emacs_toggle_background_color.html'
Created: 2010-03-20
Version: 2025-11-23"
  (interactive)
  (let* ((xstate-old
          (if (get 'xah-cycle-background-color 'xstate)
              (get 'xah-cycle-background-color 'xstate)
            (1- (length xah-cycle-colors))))
         (xstate-new (% (1+ xstate-old) (length xah-cycle-colors)))
         (xcolor (aref xah-cycle-colors xstate-new)))
    (put 'xah-cycle-background-color 'xstate xstate-new)
    (set-background-color xcolor)
    (message "background color changed to %s" xcolor)
    xcolor))

Emacs, Color Theme