4.4 Modes and interpreters

Similar to :bind, you can use :mode and :interpreter to establish a deferred binding within the auto-mode-alist and interpreter-mode-alist variables (see Auto Major Mode in GNU Emacs Lisp Reference Manual). The specifier to either keyword can be a cons cell, a list of cons cells, or a string or regexp.

The following example reproduces the default ruby-mode configuration, exactly as it is in Emacs out-of-the-box. That mode is enabled automatically when a file whose name matches the regexp "\\.rb\\'" (a file with the .rb extension), or when the first line of the file (known as the “shebang”) matches the string "ruby":

(use-package ruby-mode
  :mode "\\.rb\\'"
  :interpreter "ruby")

The default python-mode configuration can be reproduced using the declaration below. Note that the package that should be loaded differs from the mode name in this case, so we must use a cons:

;; The package is "python" but the mode is "python-mode":
(use-package python
  :mode ("\\.py\\'" . python-mode)
  :interpreter ("python" . python-mode))

Both the :mode and :interpreter keywords also accept a list of regexps:

(use-package foo
  ;; Equivalent to "\\(ba[rz]\\)\\'":
  :mode ("\\.bar\\'" "\\.baz\\'")
  ;; Equivalent to "\\(foo[ab]\\)":
  :interpreter ("fooa" "foob"))