→ coffee-mode

07 March 2010

I've fallen in love with CoffeeScript.

It's the best parts of my three favorite languages (JavaScript, Ruby, and Python) rolled into one. It runs on node.js, compiles to surprisingly readable JavaScript, has great documentation, and is written in itself.

Yes, CoffeeScript is written in CoffeeScript. Check the source code or the beautiful internals documentation.

You can use it on the server with node.js (or, presumably, any CommonJS compatible process), or you can use it in the browser.

Deployment? When you bundle your JavaScript, compile your CoffeeScript and include it. In development mode use something like rack-coffee to have your CoffeeScript automatically compiled and served to the browser - no work on your part.

There's a Vim plugin, a TextMate bundle, and, now, an Emacs mode.

Say hello to coffee-mode.

coffee-mode: An Emacs major mode for CoffeeScript.


It does all the things you want it to.

  • Syntax Highlighting
  • imenu support (for ⇧⌘T in textmate.el)
  • customize-group support
  • Comment support (alt-; and ⌘; for me)
  • Indentation using tab-width
  • "Smart" indentation (e.g. hitting RET after a -> will indent you properly)
  • Menu Bar with links to the documentation for CoffeeScript and node.js.
  • Compiles CoffeeScript

A Note on Compilation

If you're using Aquamacs, ⌘r should compile the current coffee-mode buffer and open the resulting JavaScript in a new buffer using js2-mode. You can customize the mode used to open JavaScript by overriding coffee-js-mode and you can override the key combo by adding this to the coffee-mode-hook like so:

(defun coffee-custom ()

  ;; Emacs key binding
  (define-key coffee-mode-map [(meta r)] 'coffee-compile-buffer))

(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))

Make sure you have CoffeeScript 0.5.4 or higher, or change your coffee-command and coffee-args-compile variables to whatever you know works.

Why is this worth pointing out? Because it means you can hit a single key and see the compiled JavaScript in your editor, which is just great:

Compiled Output


In your shell:

$ cd ~/.emacs.d/vendor
$ git clone git://github.com/defunkt/coffee-mode.git

In your emacs config:

(add-to-list 'load-path "~/.emacs.d/vendor/coffee-mode")
(require 'coffee-mode)

If coffee-mode is not enabled automatically for any files ending in ".coffee" or named "Cakefile", add this to your emacs config as well:

(add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
(add-to-list 'auto-mode-alist '("Cakefile" . coffee-mode))

Bugs http://github.com/defunkt/coffee-mode/issues

Source http://github.com/defunkt/coffee-mode