4

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?

  • What is your Emacs version? – lunaryorn Feb 15 '15 at 10:40
  • emacs version - 24.3.1 – Madhavan Feb 15 '15 at 11:20
  • Well, try to re-install Emacs. If a built-in package is gone, there's something wrong. – lunaryorn Feb 15 '15 at 11:26
11

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.

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.