Writing Clojure With Vim In 2013

January 16, 2013 — 3 minutes long

It’s now 2013. Vim is still my favorite text editor, and Clojure has become my favorite programming language.

A couple of recent plugins have made writing Clojure in Vim an absolute joy.

vim-clojure-static

vim-clojure-static by Sung Pae is an extraction of the excellent VimClojure plugin. But it does a few things better. Overall, it’s simpler. It leaves out functionality better served by existing Vim plugins like rainbow_parentheses.vim. It’s lightweight and the source is more accessible since it doesn’t have to include a REPL interface. What it does provide is VimClojure’s excellent syntax, indent, and filetype settings, along with some nice user settings, like fuzzy indenting and pretty multiline strings.

This sets up a great base for editing Clojure.

vim-foreplay

But alas, if one only installed vim-clojure-static, he or she would be missing out on so much. Here is where we get to the meat of Clojure development in Vim: Tim Pope’s excellent vim-foreplay. This is where we find our REPL interface, though, as Tim says, it’s “not quite a REPL”. And that’s actually perfect.

If you’ve ever tried to use a REPL interface in Vim, you’ll know it’s a dog. The Vim buffer is not meant to be a command line. vim-foreplay leverages Vim’s great buffer editing abilities instead of fighting against them. The result is something magical.

For example, say I have some Clojure code in a buffer:

( deftest test-two ( is ( = 2 2 )))

I can put my cursor over any part of that is form, then press cpp , and instantly see true at the bottom of the editor. So I know this test will pass, and I just wrote it! I could even do the same with that deftest form, and now the test-two function is instantly available for run-tests .

I could also put my cursor on that is form again, press K , and immediately see its documentation. But if the documentation is not good enough and I need to see the source — quite common in practice — I can press [<ctrl-d> and be in a buffer with the unzipped and running Clojure source.

Need a quick little prompt for a one-off command? cqp ! Or perhaps a Vim scratchpad where hitting enter evaluates the current line? cqc ! And then there’s cqq , which is just like cqc , but it will copy the form underneath the cursor into the scratchpad.

There’s even more excitment to vim-foreplay, but I’ll leave that to the reader to discover. The six aforementioned commands have spoiled me for programming with any other language. Coding in Clojure with vim-foreplay is just too much fun, probably because feedback is so immediate. It has changed the way I program and learn.

This and other works on this site are licensed under a Creative Commons Attribution-Share Alike license.