It is well-known that Emacs can select the major mode of a file depending on the file extension. It is probably lesser-known that this is only the fourth (!) step of deciding which major mode to use. Consult the "Choosing modes" section of the Emacs manual for the details. Here, I’d like to mention one particular case where I needed to look there.
I really prefer js2-mode
to the regular js-mode
, if only for its indenting capabilities. Therefore, I have this in my init.el
:
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
I was therefore quite astonished to find out that when opening some Node.js scripts (with the .js
extension), Emacs chose the default js-mode
. It turned out that the culprit was the hashbang. This way I learned about the interpreter-mode-alist
variable, and added this to my init.el
:
(add-to-list 'interpreter-mode-alist '("node" . js2-mode))
Head to the linked section of the manual to find other ways to make Emacs use exactly the mode you want it to use. (It turns out that it is a very general mechanism – you can even write your own function to select the mode!).