Joe Yates' Blog

Programming and DevOps

Editing Javascript in Emacs

Steve Yegge of Google has created a new Javascript mode 'js2-mode' for Emacs.

It works very well as it uses true parsing, not heuristics, to analyse code.


  • Download
  • Rename the file as js2-mode.el
  • Move it to a directory in your 'load-path' (~/.emacs.d/ should work),
  • Compile it:
    • Start Emacs,
    • Run the following command:
M-x byte-compile-file


Add the following to your .emacs file:

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

This associates all Javascript files with JS2 mode.

Some features of the mode are:

  • Highlighting of syntax errors and doubtful structures, e.g. missing ';', and undeclared variables,
  • The auto-indentation system works.

I often make Javascript files dynamic in Rails projects, and the only feature I would wish for is for handling of Ruby ERB tags - currently they completely confuse the parser.