I recently began experimenting with Clojure on a side project. Since Vim is my preferred editor, I’ve been learned the ropes of the prolific Tim Pope’s plugin, Fireplace. Fireplace provides documentation lookup amongst other goodies for Clojure developers. It requires a Clojure REPL for looking up docs and such, and so this REPL has to be running before Vim can connect to it.
Another aspect of my Clojure setup is Expectations. Expectations comes with a Leiningen plugin for running tests automatically, similar to using Guard to run tests in Ruby. Whilst in Ruby I can’t stand having something automatically run tests, I find it far less distracting in Clojure since I’m learning the language and need extremely rapid feedback. Plus, the Clojure tools are lightning fast – I don’t yet have any tests that I want to avoid running for speed reasons.
Given that I like to have my test output visible at all times next to my editor, and that I don’t care wildly for having a REPL visible all the time, I’ve started to use tmux to organise my workspace into a kind of IDE. The main window looks like this:
The second window has the REPL, and can be jumped to with “Ctrl-B N”.
I wanted to avoid having to manually set up the three work areas, start autoexpect and the REPL and connect to the REPL from Vim. It turns out that tmux has great scripting capabilities, and it’s really easy to automate this stuff. Here’s the script I have for opening a Clojure project from the command line:
#!/bin/bash port=8000 echo "Loading REPL on port $port" tmux new-window -d -n REPL "cd $1; LEIN_REPL_PORT=$port lein repl" while ! nc -z localhost $port; do sleep 1; done tmux split-window -d -h -p 40 tmux select-pane -t1 tmux send-keys "cd $1; lein autoexpect" C-m tmux select-pane -t0 tmux send-keys "cd $1; vim project.clj" C-m tmux send-keys ":Connect nrepl://localhost:$port" C-m C-m tmux send-keys C-p # bring up CtrlP
The script uses netcat (nc) to wait for the REPL port to be active before proceeding to set up Vim and autoexpect.
It is invoked with ‘ide $DIRNAME’. I tend to put the directory containing development projects into $CDPATH, so that I can easily change directory to any project from anywhere with ‘cd blah’. If you do this too, you can set up an IDE for a project regardless of where you’re currently at on the filesystem.
You can follow the progress of this and other bash scripts on Github.
About the Author