Setting up Emacs+Lein+Clojure in 15 minutes

30.01.2013 Permalink

Why for heaven's sake do I start using Emacs in 2013?
I have read that quite some Clojurians prefer Emacs over other IDEs so I gave it a shot and trained myself the necessary keystrokes until I felt comfortable. Now Clojure-Mode and Paredit really relieve my eyes and my head after having tried Counterclockwise for a while. But it took me about 2 hours to figure out the settings for Emacs to properly support Clojure development. If you're used to Eclipse and just want to experiment with Clojure then Counterclockwise might be the natural first approach as you avoid learning Emacs + Clojure at the same time.

So here's the recipe for Debian based distros like Ubuntu or Linux Mint.

  1. Ensure you have Java 7 or better 8 installed.
  2. Download the Leiningen Script. Leiningen is the central build and development tool for Clojure. Follow the simple instructions.
  3. For Emacs add the snapshot PPA by issuing sudo apt-add-repository ppa:ubuntu-elisp/ppa in a terminal window.
  4. To actually install Emacs type sudo apt-get install emacs-snapshot emacs-snapshot-el
  5. You find my ~/.emacs.d contents on GitHub, to get it use git clone ~/.emacs.d
  6. Start Emacs, wait until install is complete and restart Emacs.
Clojure nREPL integration is provided by CIDER. To make full use of it, the Leiningen cider-nrepl plugin must be present, either in project.clj or ~/.lein/profiles.clj. Here's my profiles.clj contents as example.

Ok, the infrastructure is now set up. To get started with a new Hello World project:

  1. Type lein new helloworld. If this is the first start of the lein script then it will download some stuff before actually creating a new project skeleton.
  2. Start Emacs.
  3. Load a file into the left buffer: C-x C-f ~/projects/helloworld/src/helloword/core.clj.
  4. Type C-c M-j (Emacs parlance for Ctrl-C and Alt-J on PC keyboards). This starts a JVM REPL process and connects Emacs with it.
  5. In the cider-repl buffer switch to the helloworld namespace with the command (ns helloworld.core).
  6. Switch the input focus using C-x o. Type C-c C-k to load, compile and evaluate the currents buffer code into the REPL process.
  7. Switch back to the cider-repl buffer with C-c C-z or C-x o. Invoke the foo function with (foo "Hey!"). You're ready to play.
I found this setup useful, yet minimal. Find my favorite keyboard shortcuts on GitHub.