Twitter GitHub Facebook Instagram dirv.me

Daniel Irvine on building software

Paredit for Vim Clojure development

23 October 2014

Paredit.vim is an indispensable Vim plugin for Clojure development. If you’re a Vim user and you’re beginning Clojure, you should be using it. If you’re an Emacs user and you’re beginning Clojure, you should be using the ParEdit minor mode.

Parens explosion

The LISP family of languages are parentheses-based, and that means they look very odd for anyone coming from OOP or procedural languages. Here’s a Clojure code example.

(defn play-move [board square mark]       
   (let [bv (vec board)]                  
     (if (= \- (nth bv square))     
       (apply str (assoc bv square mark)) 
       board)))

There’s a whole lot of parens floating about, and as a Clojure developer you’ll be writing a lot of them. The language is homoiconic: data and code are represented in the same way. Additionally, the language uses prefix notation, and prefix notation does not suffer operator precedence amibiguity like infix notation. Together, Clojure’s homoiconicity and prefix notation means is all of these familiar tokens...

;  :  ?  (  )  ,  {  }  =  begin  end 

...well, they boil down to just two:

(  )

... and that’s why there’s a parens explosion within your code.

Why Paredit?

Oddly enough, these parens aren’t for your benefit, they’re for the compiler’s benefit. It would be hard to argue that parens make your code easier to read. What actually helps you is whitespace.

Look again at the Clojure code example above. How much does the whitespace--the newlines and the tab breaks--aid your understanding of the code? Actually, quite a lot. We can postulate that the parens help the compiler and the whitespace helps the programmer.

It’s unfortunate that any programmer coming to Clojure will likely find themselves getting lost in parentheses and, without a doubt, will be left fixing many an unmatched parenthesis error.

That’s where Paredit comes in. It writes the parentheses for us, ensuring that our document is always syntactically valid, at least when it comes to matching parentheses. Not only does it write parentheses, but it includes commands to join, split, wrap and move elements in the syntax tree. That means that, for the most part, you can stop worrying about syntax entirely.

Knowing the syntax tree

Once you no longer have to worry about getting the syntax right, you can concentrate on the real task: building your program’s syntax tree. Using Paredit helps because you’ll start thinking about your code as nodes in a tree, and its editing commands work the way you’d expect: create a new parent node, split a node into two sibling nodes, and so on.

In conclusion, I couldn’t imagine writing Clojure without Paredit. Try it out if you haven’t already.

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram dirv.me