Emacs: How to Set a Theme Depending on Mode?

By Xah Lee. Date: . Last updated: .

there's no way to do this properly, because background color is associated with window (what emacs calls “frame”), not mode.

Here's how you can set background color depending on the file name extension.

;; sample code for setting a background color depending on file name extension

(defun my-set-theme-on-mode ()
  "set background color depending on file suffix"
  (let ((fileNameSuffix (file-name-extension (buffer-file-name) ) ))
     ((string-equal fileNameSuffix "el" ) (set-background-color "honeydew"))
     ((string-equal fileNameSuffix "txt" ) (set-background-color "cornsilk"))
     (t (message "%s" "no match found"))

(add-hook 'find-file-hook 'my-set-theme-on-mode)

You can modify the code to set themes instead of just background. See: Emacs: Set Color Theme.

In the same way, hook is used to customize keys for major mode. [see Emacs: Change Major Mode Keys]

If you want change based on major mode instead of file name extension, you'll need to find the major mode's name. [see Emacs: Set Default Major Mode]

List of Major Modes and File Name Extension

To find out what major mode's name is, Alt+x describe-variable than type major-mode.

Here's a basic map of major-mode's names and file name extension.

(defvar xah-html-lang-name-map nil "a alist that maps lang name. Each element has this form 「(‹lang code› . [‹emacs major mode name› ‹file_extension›])」")
(setq xah-html-lang-name-map
           ("ahk" . ["ahk-mode" "ahk"])

           ("code" . ["fundamental-mode" "txt"])
           ("output" . ["fundamental-mode" "txt"])

           ("bash" . ["sh-mode" "sh"])
           ("bash-output" . ["fundamental-mode" "txt"])
           ("unix-config" . ["conf-space-mode" "conf"])
           ("cmd" . ["dos-mode" "bat"])

           ("bbcode" . ["xbbcode-mode" "bbcode"])
           ("c" . ["c-mode" "c"])
           ("cpp" . ["c++-mode" "cpp"])
           ("common-lisp" . ["lisp-mode" "lisp"])

           ("org-mode" . ["org-mode" "org"])

           ("clojure" . ["clojure-mode" "clj"])
           ("css" . ["css-mode" "css"])
           ("elisp" . ["emacs-lisp-mode" "el"])
           ("emacs-lisp" . ["xah-elisp-mode" "el"])
           ("haskell" . ["haskell-mode" "hs"])
           ("html" . ["html-mode" "html"])
           ("mysql" . ["sql-mode" "sql"])
           ("xml" . ["sgml-mode"])
           ("html6" . ["xah-html6-mode" "html6"])
           ("java" . ["java-mode" "java"])
           ("js" . ["js-mode" "js"])
           ("lsl" . ["xlsl-mode" "lsl"])
           ("ocaml" . ["tuareg-mode" "ocaml"])
           ("org" . ["org-mode" "org"])
           ("perl" . ["cperl-mode" "pl"])
           ("php" . ["php-mode" "php"])
           ("povray" . ["pov-mode" "pov"])
           ("powershell" . ["powershell-mode" "ps1"])
           ("python" . ["python-mode" "py"])
           ("python3" . ["python-mode" "py3"])
           ("qi" . ["shen-mode" "qi"])
           ("ruby" . ["ruby-mode" "rb"])
           ("scala" . ["scala-mode" "scala"])
           ("scheme" . ["scheme-mode" "scm"])
           ("yasnippet" . ["snippet-mode" "yasnippet"])
           ("vbs" . ["visual-basic-mode" "vbs"])
           ("visualbasic" . ["visual-basic-mode" "vbs"])
           ("mathematica" . ["fundamental-mode" "m"])

           ("slim" . ["slim-mode" "slim"])
           ("yaml" . ["yaml-mode" "yaml"])

           ) )

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial