js2-mode

I’ve started having to do some javascript editing with Emacs for work, and so I installed Steve Yegge’s js2-mode. You can read his description of the mode here. His description is fairly extensive, so you should probably read it instead of or in addition to mine. The small amount I’ve used it has made me like it more than most programming modes Emacs has.

js2-mode, unlike most other modes, comes with a javascript parser. This allows it to know much more about the code, since it can pull eclipse-like tricks of highlighting errors and warnings in your code. Flymake-mode does this for other languages, but it’s not nearly as well integrated as js2-mode’s system. If you have an error, the code causing the error is underlined in red with a message in the minibuffer when your point is over it; if a warning, underlined in orange. This is incredibly useful.

js2’s indentation is more like python-mode’s indentation than cc-mode. It isn’t customizable, which hasn’t so far come up as a problem for me, but if you have very strict code style guidelines you may have to do some indenting manually. It provides several options for cycling through possible indentations, and one of them is usually right. Still, it can be somewhat irritating that it doesn’t just indent correctly all the time.

js2 does a few other things that are quite nice. Pressing enter inside a string will extend it to the next line, which is pretty much always what I want to do. I haven’t used it very much - I’ve mostly just been doing very like editing with it - but it is very customizable through the customize mechanism. It also supports hiding of blocks, which I don’t tend to use, but to each their own.

js2-mode is going to be built-in to GNU Emacs 23.2, but if you don’t want to wait until then to use it you have to download and enable it yourself. To enable js2-mode in your emacs, first you must download and byte-compile js2.el from here. If you don’t byte-compile the file, you’ll have to set js2-mode-must-byte-compile to nil in order for it to work at all, but byte compiling the file will make it work much faster. Once that is done, put the following lines in your initialization file to start js2-mode for all .js files:

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

Tags: ,

3 Responses to “js2-mode”

  1. Eisen says:

    How come Yegge is the only person whose name is always mentioned in conjunction with his mode? He must be quite good at self marketing I guess…

  2. Chad says:

    While js2-mode is currently in the emacs CVS repo, there was a massing thread on the emacs mailing list (http://lists.gnu.org/archive/html/emacs-devel/2009-08/msg00395.html) that debates whether it will be included. In all likelihood, it won’t be included precisely due to the fact that js2-mode includes a javascript parser (which is very un-emacs mode-like - according to some). It appears that the next javascript mode to be included in emacs will be a merge of js2-mode and a javascript mode called espresso.

  3. nflath says:

    I saw that thread - it seemed to me like it would be left into Emacs while an asynchronous parser was developed, but I suppose we’ll see when 23.2 comes along.

Leave a Reply