Editing JavaScript in Emacs
Let’s skip right to the chase: I recommend using Steve Yegge’s js2 mode.
If you want to read about the other options and how they compared with one another in September 2005, read on!
You may also be interested in other things I’ve written about Emacs or on JavaScript.
If you find yourself doing substantial hacking on complicated websites, you’ll probably find yourself needing a decent major mode for editing JavaScript. There are at least six different JavaScript major modes out there — which one should you use? Let’s take a look at them!
A survey of the various JavaScript major modes
generic-x.el (which comes with Emacs)
contains javascript-generic-mode
, which
doesn’t handle indentation well, and fails to correctly
highlight C-style (/* … */
) comments. But
it’s fine for short-term use: since it ships with Emacs,
it’s already there when you need it.
To enable it, add (require 'generic-x)
to ~/.emacs
. This will automatically
associate files with .js
extensions with
javascript-generic-mode
.
Peter Kruse’s
javascript-mode.el is antiquated. It
hasn’t been updated in many years, and relies on
hilit19
, an obsolete syntax highlighting library.
Definitely not recommended.
My recommendation: Karl Landström’s
javascript.el
appears to be the nicest JavaScript mode out there. It handles
indentation well, and both C- and C++-style (//
…
) comments. A winner! To use, place javascript.el
in a directory inside
load-path
and add the following to your ~/.emacs
:
(autoload 'javascript-mode "javascript" nil t)
(add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode))
Joost Diepenmaat has a slight
tweak of Karl’s mode which contains
a few font-lock fixes for quoted strings and regular
expressions.
I haven’t tried it myself.
There’s also ecmascript-mode.el,
which is a derived mode from java-mode
. It
doesn’t appear to do anything that javascript.el
doesn’t do, and it
unconditionally makes font-lock-builtin-face
bold,
regardless of your personal settings. Thanks to krupan and
Mathias for the link.
MozRepl comes with its own js-mode.el, which appears to be a very
minimal variant on cc-mode
. Thanks to RetroJ for
pointing this out.
In addition to all of the above, there’s the
javascript-mode.el
which comes
with XEmacs, which I’m told works in Emacs as well
(and is even bundled with Aquamacs). Thanks to Reed and Xah for
the link.
Robustness tip: simple failover
Say you’ve installed javascript.el
on machine A but not on
machine B. Ideally, your ~/.emacs
file should detect if javascript.el
is installed: if it is, it
should use it, but if it isn’t, it should fall back on
javascript-generic-mode
from generic-x.el
. This is pretty easy stuff.
First, require
generic-x
as above.
Then check for the presence of javascript.el
, and configure it if
it’s present:
(require 'generic-x)
(when (locate-library "javascript")
(autoload 'javascript-mode "javascript" nil t)
(add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode)))
This way, you can copy your ~/.emacs
file to some other machine, and
you know it won’t break.