Learning Clojure: Setting up the Emacs on OSX

I’ve been using the Emacs since my Lisp and AI course back in 1987. (My use of the definite article betrays that :)) So, of course I wanted to learn Clojure using it. Unfortunately, there are either incomplete or conflicting posts in googlespace on how to go about doing this. You *can* grab the source with git and build etc., but let’s try the simplest thing possible.

xkd comic

There are two ways to have the Emacs interact with Clojure: inferior-lisp-mode and Slime. I will cover both here.

Initial setup

Clojure runs on the JVM (there are separate projects for the CLR and also ClojureScript), so you should have a JDK version 1.5 or later installed. If you have not set up Leiningen, or even know what that is, please read the first article in this series on setting that up.

Emacs

I was using Emacs 23. It is a bit more difficult setting it up and managing 23 since integration with the Emacs Lisp Package Archive isn’t baked in. So, just get Emacs 24.

For OSX, get the latest Emacs 24 pretest build. I – and many others – have been using it with no problems in spite of it not being a “release.”

Of course I had my Emacs init files. You may have either a ~/.emacs or ~/.emacs.d/init.el lying around. I renamed them (e.g. .emacs.d.bak) to start fresh and then launched Emacs. Just a reminder, if you have a ~/.emacs file, ~/.emacs.d/init.el will be ignored.

I edited my new ~/.emacs.d/init.el to include an additional elisp repository which hosts clojure-mode and paredit.

Go ahead and evaluate the buffer (M-x eval-buffer).

The sequence of Emacs commands follows. You first refresh the local list of available packages and writes that list into a directory in your ~/.emacs.d. This will take bit of time. Then you install the clojure-mode package.

If you want, you can run M-X package-list-packages to see what’s there, but I suggest that you don’t blindly add packages without first knowing if they play nicely together.

Inferior Lisp mode

Edit your ~/.emacs.d/init.el again and add these lines. If you’re not on OSX, ignore the third line which changes the Meta key from the Option key to the Command key.

Now we can launch a REPL in a subprocess within the Emacs. This is Inferior Lisp Mode.
Open up a test clojure file (e.g. C-x f foo.clj), or navigate to a lein project you created in the first article in this series and edit or create one there.
Enter M-x run-lisp (or C-c C-z).
This will tell lein to start a REPL, since that is what you set the variable inferior-lisp-program to.
Now split the window by typing C-x 2. In one of the windows, navigate to your Clojure file.
Enter a simple sexp. Then after the final ), type C-x C-e to evaluate it. (clojure-mode binds this key sequence to lisp-eval-last-sexp, as well as C-c C-e so you don’t have to remember which!. As always, you can type M-x m for mode info.) You will see the result in the *inferior-lisp* REPL buffer.

You can also go into the REPL buffer and type expressions there and they will be evaluated. Editing is fairly basic, but it may be all that you need. Indeed, if you’ve seen the screencasts by Rich Hickey,the creator of Clojure, you will see that he is using inferior Lisp mode.

Clojure mode key bindings.

 

inferior-lisp

Slime

The Superior Lisp Interaction Mode for Emacs (Slime) has many more features, including a debugger, and code completion. It was written for other Lisps and is based on a client server model. Slime is the Emacs client and Swank is the “server”. That means you can run Clojure on a different machine if you’d like. For this article, we’ll stick with using it locally.

Swank

Since you already are using Leiningen, simply add the swank plugin. It will be downloaded to ~/.lein/plugins.

You can check current versions like this:

Navigate to your project: using dired is easiest. Then do this:

This starts a Swank server, then uses Slime to connect to it from Emacs.
If you get an error stating that lein cannot be found, see OSX quirks.
Slime’s keybindings are a bit different from Inferior Lisp mode. I suggest that you type M-x m and scroll down to the Slime section or read the Swank Clojure Readme.

Since your are still learning Clojure, Slime’s inspector is quite handy. Place the cursor after a symbol or expression and type C-c I (I as in Inspector). The minibuffer will ask for confirmation and then display the result.
The other handy exploration function is slime-edit-definition. Place the cursor after a function, e.g. after the + in (+ 2 2), then type M-. (meta period). The source code for the definition of + will appear in a buffer.
Visit Slime’s home page for more information. There is a screencast there now showing it in action with Common Lisp.

slime

Alternatives

There are two alternative ways of specifying that you want to use Swank: through a project independent profile and inside a project.

For the global profile, add this to ~/.lein/profiles.clj

Or add this to your project.clj if you want it inside your project. I prefer to not do this since other team members might be using another editor.

OSX quirk

Here are a few of my OSX specific entries from .emacs.d/init.el. I prefer to use the command key as Emacs’ meta key. If you placed the lein script outside of “normal” paths, (e.g. ~/bin) you may have a problem with lein not being found inside Emacs. If that is the case, you can add your PATH that you set in .bash_profile to Emacs.

Emacs Starter Kit

I suggest that you also install Phil Hagelberg’s Emacs Starter Kit. It includes a good set of defaults, as well as ido-mode and paredit.

If you don’t want the starter kit, at least install paredit.

Then add this to your ~/.emacs.d/init.el

Of course the starter kit does this for you. Read the documentation at the Emacs Wiki on how to use paredit mode.

Have fun.

Resources

Books

Share These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Facebook
  • Twitter
  • LinkedIn
  • email
  • DZone
  • Slashdot
  • Reddit
  • Google Bookmarks
  • Digg
  • StumbleUpon
  • del.icio.us
This entry was posted in Clojure, IDE and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted February 20, 2013 at 8:49 am | Permalink

    Hi there i am kavin, its my first occasion to commenting anywhere, when i read this paragraph i thought i could also make comment
    due to this brilliant article.

One Trackback

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">