5

I enabled js2-mode in emacs by adding this snippet in .emacs,

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

But still for .js files, emacs tries to load javascript-mode alias js-mode as the major mode. And god knows, how did javascript-mode function go missing in my emacs. It crashes like this,

Debugger entered--Lisp error: (error "Autoloading failed to define function javascript-mode")
  javascript-mode()
  set-auto-mode-0(javascript-mode nil)
  set-auto-mode()
  normal-mode(t)
  after-find-file(nil t)
  find-file-noselect-1(#<buffer a.js> "~/a.js" nil nil "~/a.js" (1840059 2049))
  find-file-noselect("/home/sk/a.js" nil nil)
  ido-file-internal(raise-frame)
  ido-find-file()
  call-interactively(ido-find-file nil nil)

This leads to another question, does emacs consider the last (or) first regex entry in auto-mode-alist for loading the major mode?

Madhavan
  • 1,957
  • 12
  • 28

1 Answers1

13

There is a trailing quotation mark in your regular expression, so it will never match files with the .js extension. Use the following regular expression instead:

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

Note the absence of the dollar—which matches the end of a line rather than of the string—and the backslash before the quotation mark: \\' properly matches the end of a string.

I'd recommend you to use the rx macro for regular expressions, which makes them more readable:

(add-to-list 'auto-mode-alist `(,(rx ".js" string-end) . js2-mode))

Furthermore, I'd recommend you to reinstall Emacs completely. If a built-in package like javascript-mode is gone, there might be something fundamentally broken in your installation.