10 Comments

Rigging up Sublime Text for Clojure Development

I recently started working on a project using Clojure. Since it’s my first experience with the language, I decided to invest a bit a of time setting up Sublime Text so that my editor would be tuned to conquer the oh-so-many parens and the build-and-test workflow provided via Leiningen. I wanted to capture and share this setup to help others get off the ground more quickly, since it took a bit of hunting for plugins and fiddling with Sublime.

Basic Setup

My setup is based on Sublime Text 3, though the general ideas and plugins should work for Sublime Text 2, as well. Though Sublime Text 3 is still technically still in beta, it is very stable, and most users have migrated to it. I would recommend making the jump, since ST3 has some nice new features built in, like navigation to definitions of functions and variables.

One special tweak is needed for selecting identifiers in Clojure, since unlike many languages, it allows identifiers to contain the hyphen (-) character. In order to fix this, you can do the following:

  1. Open any Clojure source file (for example, foo.clj)
  2. Goto: “Sublime Text->Preferences->Settings – More->Syntax Specific – User”
  3. Paste the following into the Clojure.sublime-settings, and save the changes:
    {
        "word_separators": "./\\()\"':,.;<>~!@#$%^&*|+=[]{}`~?"
    }

Tackling Those Pesky Parens and Brackets

In LISPy languages, parentheses must wrap any statement/context, and the necessary nesting leads to streams of closing parens that can cause much frustration. Sublime’s built-in support for identifying the matching paren or bracket is actually fairly decent, but the way Sublime highlights the bracket isn’t quite obvious enough to the eye at times.

After searching through plugins supplied via the invaluable Package Control plugin, I found BracketHighlighter does the trick and even supports multi-cursors, as well. It makes the matching bracket underscore much more prevalent and also shows an image of the actual bracket type being matched in the gutter where the line numbers show up.

Bringing the Rainbow Magic

While this plugin is great, using rainbow bracket plugins to help visualize the nesting has become a popular option, as well. It took me a while to track it down, but I finally found the Rainbowth package–though it did take a bit of head scratching to get it to work properly. Rainbowth does its bracket magic by by actually modifying your color scheme file due to limitations in the plugin API in Sublime. The dilemma here, though, is that the color schemes are not easily edited by a plugin, since they are stored in ZIP archives in the Sublime Text installation folder.

In order to get the plugin to work with a given color scheme after installing it, you need to extract the desired color scheme into your user Packages folder so it can be edited by Rainbowth. For example, In OSX, you would do the following:

  1. In a terminal, go into the Sublime Text app bundle: > cd /Applications/Sublime\ Text.app/
  2. Make a copy of the color scheme archive file to get the color scheme out of it: > cp Contents/MacOS/Packages/Color\ Scheme\ -\ Default.sublime-package ~/
  3. Extract the scheme into a temporary location: > cd ~/; mkdir tmp; cd tmp; unzip ../Color\ Scheme\ -\ Default.sublime-package
  4. Copy the desired scheme into your user Packages folder (e.g Monokai): > cp Monokai\ Bright.tmTheme ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/

You should then be ready to roll with Rainbowth. I did discover a rendering bug that causes the colorized brackets to slice a sliver off of the preceding character, which is a bit annoying. Hopefully, this will be fixed at some point.

Rainbowth Example

Rock the REPL

When developing in Clojure, the REPL is your main tool. It’s how tests are run and also auto-run upon source files changing. Using a separate terminal window is definitely a viable solution, but switching between windows may be a bit annoying for some.

Though it isn’t perfect, SublimeREPL lets you actually open the Leinengen REPL right in a Sublime window! There are a couple of quirks, but it’s definitely worth trying it out.

Screen Shot 2016-03-20 at 11.46.50 PM

Hope that works for you!