Hi, I'm Nick. This is my blog. I'm a life-long unschooler living in New York. You can find more about me here.

I help run the Recurse Center (YC'S10).

Follow me @nicholasbs

How to setup Emacs for Clojure on Mac OS X Lion

October 22, 2011

These instructions should get you going with an Emacs environment for writing Clojure on Mac OS X Lion. (This should work on older versions of OS X, but I've only tried it on 10.7.)

  1. Install Homebrew. Homebrew is an awesome package manager for OS X. You can obviously skip this step if you already have it installed. Otherwise, you can install it by reading these instructions or simply running:

    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
  2. Install a newer version of Emacs. Lion comes with an old version of Emacs (22.1.1) by default. You'll need at least version 23. Run emacs --version to see what version you have. Homebrew currently has version 23.3.1, which you can install by running:

    $ brew install emacs

    Run emacs --version to make sure you've got a newer version now. (If not, try running brew update to get the latest package information and then brew upgrade emacs to install the latest packaged version.)

  3. Install Package, an Emacs package manager

    Create an .emacs.d directory if you don't already have one and a folder to store packages in:

    $ mkdir ~/.emacs.d $ mkdir ~/.emacs.d/packages

    Create (or edit) your ~/.emacs.d/init.el file and put the following in it:

    (add-to-list 'load-path "~/.emacs.d/packages/")
    (require 'package) (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/")) (package-initialize)

    Put package.el in ~/.emacs.d/packages/.

  4. Install clojure-mode and paredit. Open Emacs and run:

    M-x eval-buffer M-x package-refresh-contents M-x package-install clojure-mode M-x package-install paredit
  5. Install Leiningen and swank-clojure.

    $ brew install leiningen $ lein plugin install swank-clojure 1.3.3
  6. Finally get a Clojure REPL in Emacs. You can create a Clojure project with Leiningen with the following command (replace myproject with your project name):

    $ lein new myproject

    When editing a .clj file in Emacs you can run the following to get a REPL:

    M-x clojure-jack-in

You should now have syntax highlighting, proper indentation, file navigation, and an enhanced REPL for Clojure in Emacs. You can learn more about using the REPL here.