Clojure files should open in Clojure-mode. I also have Clojure files that contain color info and are used to generate my styles; they live under a /styles path. For these files, I want them to have both clojure-mode (a major mode) and rainbow-mode (a minor mode). I can implement this on a file-by-file basis be starting such files with a line like this:

;; -*- eval: (rainbow-mode) -*-

But I make and use enough of these files, I tried the following:

(use-package rainbow-mode
:mode "css\\|style")

Which adds that line to the top of auto-mode-alist. But that causes the auto-mode to no longer match my clojure files if they dwell under a style/* directory; it enables the rainbow minor mode and stops there. Is there a simple solution for getting both of these modes to load on clj files without having to preface every such file with the eval line?

1 Answer 1


To use rainbow-mode in clojure-mode only under a certain directory, add a .dir-locals.el file to the directory, containing:

((clojure-mode . ((eval . (rainbow-mode 1)))))

You can also use M-x add-dir-local-variable to create/edit the file.

Note that eval is a pseudo-variable used to evaluate elisp.

Alternatively, with the file in the project root:

(("path/to/subdirectory" . ((clojure-mode . ((eval . (rainbow-mode 1)))))))

Refer to C-hig (emacs)Directory Variables for more details, as well as an equivalent way of specifying this in your init file rather than with a .dir-locals.el file.

To use rainbow-mode in clojure-mode whenever the buffer file name contains /style/ or /css/:

(add-hook 'clojure-mode-hook #'my-clojure-mode-hook)

(defun my-clojure-mode-hook ()
  "Custom `clojure-mode' behaviours."
  (and buffer-file-name
       (string-match "/\\(?:style\\|css\\)/" buffer-file-name)
       (rainbow-mode 1)))
  • I guess there's no harm in turning on rainbow mode in every Clojure buffer, but I was hoping to have it only in those under "style" or "css" directories like with the automode functionality Jun 10, 2021 at 1:35
  • Sorry, I didn't grok the question properly. You can do that with directory-local variables. I'll update the question.
    – phils
    Jun 10, 2021 at 1:59
  • 1
    You can also alter my-clojure-mode-hook to check on the buffer's directory and only execute the (rainbow-mode 1) is in a relevant one.
    – mmmmmm
    Jun 10, 2021 at 8:52
  • @mmmmmm Good idea. I've made that change.
    – phils
    Jun 10, 2021 at 11:30
  • Solves the problem and also demonstrates a cool use of (and) to make an if/when unnecessary! Jun 10, 2021 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.