- Published on
Switch theme in emacs
- Show available themes to select
- Disable the current theme
- Load the selected theme
(defun switch-theme ()
"Interactively select and load an Emacs theme."
(interactive)
(let* ((available-themes (custom-available-themes))
(current-theme (car custom-enabled-themes))
(chosen-theme (intern (completing-read "Select theme: " available-themes nil t)))
(disable-themes (delq chosen-theme custom-enabled-themes)))
;; Disable the current theme(s)
(mapc 'disable-theme custom-enabled-themes)
(setq custom-enabled-themes nil)
;; Load the chosen theme
(load-theme chosen-theme t)
(message "Theme '%s' loaded. Disabled themes: %s" chosen-theme (mapconcat 'symbol-name disable-themes ", "))))
Here's how the function works:
available-themes
: It retrieves a list of available themes.current-theme
: It captures the currently enabled theme.chosen-theme
: It allows to select a theme from the available themes usingcompleting-read
.disable-themes
: It stores the list of themes that need to be disabled (the current theme plus any other currently enabled themes).mapc
: It disables the themes listed indisable-themes
.setq custom-enabled-themes nil
: This clears the list of enabled themes.load-theme
: It loads the chosen theme.- A message is displayed indicating which theme was loaded and which themes were disabled.
(switch-theme)
, will prompt to select a theme from the available themes, disable the current theme(s), and load the selected theme.