VS Code recently added the ability to switch themes when macOS's dark mode changes. Presumably they use the Electron API, which internally uses the Objective-C API of subscribing to the AppleInterfaceThemeChangedNotification event in NSDistributedNotificationCenter.

It would be nice if Emacs had this same ability. The pseudo-code would be:

(add-event 'os-dark-mode-changed
  (lambda (on)
    (set-theme (if on

(In VS Code, the dark and light themes are configurable.)

Does Emacs have the ability to do something similar?


Using emacs-plus this is easy. See the docs, but here is the code in any case.

(defun my/apply-theme (appearance)
  "Load theme, taking current system APPEARANCE into consideration."
  (mapc #'disable-theme custom-enabled-themes)
  (pcase appearance
    ('light (load-theme 'tango t))
    ('dark (load-theme 'tango-dark t))))

(add-hook 'ns-system-appearance-change-functions #'my/apply-theme)

A work-around, more so than an answer: you can change your theme from an external process, by starting your main editor instance in server mode, and using emacsclient. E.g.



emacsclient --eval "(my/dark-mode)" --quiet -no-wait --suppress-output -a true

It turns out that iTerm2 allows you to execute an arbitrary Python script on OS theme changes. You can use this to change iTerm2's own theme, but nothing stops you from adding a cheeky call to emacsclient and controlling Emacs along, with it.

I've combined all of this here: https://gist.github.com/hraban/8eb4ab5c110828a9d1b8c16b4f78193e.

Upside: no polling, no Emacs UI blocking, etc

Downside: iTerm2 must be running.

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.