9

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\\'"
  :init
  (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.

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

4 Answers 4

10

Use

: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.

15

The following should work:

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

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

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

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.

0

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\\'"
         "\\.jsx\\'"))

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\\'"
         "^Jenkinsfile$"))

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 that you have read and understand our privacy policy and code of conduct.

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