When using use-package, is there a way to associate more than one file extension with a particular mode in one place instead of multiple locations?

Case in point is that I would like to associate both js and jsx files with js2-mode. Currently what I have is:

(use-package js2-mode
  :mode "\\.js\\'"
  (setq js2-highlight-level 3) 
  (add-hook 'js2-mode-hook (lambda () (auto-revert-mode)
                                      (setq auto-revert-interval 0.1)))
  :ensure t)

(add-to-list 'auto-mode-alist '("\\.jsx\\'" . js2-mode))

... which I don't like as the extensions are specified in two different places.

  • 7
    Couldn't you just turn your regexp in to \\.jsx?\\' to account for both filenames?
    – nispio
    Commented Mar 23, 2016 at 19:34

4 Answers 4



:mode "\\.jsx?\\'"

Remember: all those backslashes are there because it's a regexp, so you really want to take advantage of it when you can, otherwise you're just suffering through those backslashes for nothing.

You could also write it

:mode "\\.\\(js\\|jsx\\)\\'"

or many other ways, of course.


The following should work:

(use-package js2-mode
  :mode (("\\.js\\'" . js2-mode)
         ("\\.jsx\\'" . js2-mode))
  :ensure t)

In addition to scatotonic's way you can use this shorter equivalent expression:

(use-package js2-mode

It's not as useful when only doing the two entries, (especially since they can be merged with the regex "\\.jsx?\\'"), yet it's really helpful for more generic stuff like web-mode.


It's pretty dang flexible... This works, and is my preferred method when the mode name is the same as the package name:

(use-package js2-mode
  :mode ("\\.js\\'"

I do see why a regex like in Stephan's answer would be nice for the js/jsx case. Here's an example where a single regex may be too complicated and a list of regexs may be preferred:

(use-package groovy-mode
  :mode ("\\.groovy\\'"

scatotonic's answer would be my choice if the mode name is different from the package name.

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.