Clojure Development in Spacemacs

Article summary

I’ve recently been doing some basic Clojure development, and it’s been a huge blast. I’ve played around with it in the past, but never bothered to fully set up a development environment. This time, however, I decided to spend a few evenings perfecting my workflow and becoming familiar with the tools available, focusing on my editor of choice: Spacemacs.

Spacemacs is an awesome editor that can greatly improve your productivity. I have been a Spacemacs user for quite some time now, and it has proven to be excellent for Clojure. Check out this post for more information. As Emacs is primarily written in Lisp, and Clojure is a dialect of Lisp, the two go hand-in-hand.

Today, I’m going to briefly describe the installation and setup process and point out a few tools that I’ve found useful.

Prerequisites

You will need:

Leiningen can be installed manually or through a package manager. The manual approach is straightforward and recommended by the creators–the instructions are on the website and the repository. Regardless of how you install Leiningen, you should be able to run command -v lein and see the install location.

Installing Spacemacs is a little less straightforward, only because there are many more ways to get Emacs. I personally prefer a stand-alone client, however, you might want to access it from the command line, or you might want both. If you decide to download the client, aliasing /Applications/Emacs.app/Contents/MacOS/bin/emacsclient --no-wait is very helpful. Otherwise, the Spacemacs documentation recommends using your preferred package manager (such as Homebrew) to install Emacs. Just make sure that you’re running Emacs 24.4 or up.

Spacemacs is quite easy to install, so just follow the instructions on the website or repository.

The Setup

Once you have Spacemacs and Leiningen installed on your system, fire it up and access the .spacemacs file, easily accessible via SPC f e d.

Throw ‘clojure’ in the dotspacemacs-configuration-layers list, and press SPC f e R to install it. Spacemacs layers are essentially a collection of ELPA packages, and the Clojure layer includes several, notably cider, clojure-mode, and smartparens.

And that’s it! You’re all set up for Clojure development in Spacemacs!

Features

One thing that separates Emacs Clojure development from others is that you can start up REPLs directly in the editor. Other editors, such as Vim, require a separate Clojure REPL to be running before they can connect to it. If you prefer that method, you still have the option to connect to a running REPL.

Getting up and running quickly is another major benefit, especially if you have used Spacemacs in the past. The discoverability of functionality is quite easy, particularly within Clojure-mode, as the patterns tend to stick–e.g. buffer, function, expression, etc.

SPC m and , both bring up the clojure-mode menu. This is where you can find available features, if you don’t know the key sequence off the top of your head.

Pressing SPC m brings up the major-mode menu.
Pressing SPC m brings up the major-mode menu.

Here are several commands that I’ve found invaluable:

  • SPC m s i: creates a CIDER REPL and connects to it. I always have one running when I’m writing any Clojure code.
  • SPC m h n: brings up a menu to browse the namespace. I primarily use this to access Clojure.core, to see a list of available functions, and to quickly navigate to the source code.
  • SPC m e b: evaluates the buffer, SPC m e f evaluates the function, and SPC m e f evaluates the selected region in the REPL.
  • SPC m s b: loads the buffer in the running REPL, which is very handy for testing several functions.
  • SPC m s f: loads the function at the cursor in the running REPL.
  • SPC m t a: runs all of the tests in the namespace, while SPC m t t runs the test under the cursor.

Pressing SPC m s f sends the function to the running REPL
Pressing SPC m s f sends the function to the running REPL.

I’ve only just started to scratch the surface of Clojure development, and I’ve only used a fraction of the capabilities that Spacemacs provides. If you do any form of Clojure development, I would encourage you to try out Spacemacs.