6

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.

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

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.

11

The following should work:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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