How I develop Clojure with Vim
Recently Lau Jensen wrote a post talking about the features of Emacs and why it increases the productivity of Clojure programmers. While I don’t disagree that lisp programming in general benefits greatly from using Emacs as an editor, there are simply people who are too heavily invested into Vim (like myself) for things like viper-mode to work for them. So, I thought I’d share how I do Clojure development with Vim. Throw in my 2 cents.
The key (for me) to editing Clojure code in Vim is a combination of two plugins, VimClojure and slime.vim (see associated blog post). One of the difficult things is that slime.vim doesn’t actually exist anywhere on vim.org’s list of scripts, so it has to be downloaded from the aforementioned blog post. Stick it in the ~/.vim/plugins directory to install it.
First, VimClojure. I tend not to use Nailgun at all, some people like it, I don’t. So instead of the regular install for vimclojure, I copy over the files from the autoload, doc, ftdetect, ftplugin, indent and syntax folders to their respective Vim folders. If you think you’ll want the Nailgun functionality, you should use the installation instructions provided by Kotarak.
Now, add the settings you need for VimClojure to your .vimrc:
" Settings for VimClojure
let g:clj_highlight_builtins=1 " Highlight Clojure's builtins
let g:clj_paren_rainbow=1 " Rainbow parentheses'!
I have to say, rainbow parentheses’ is one of the best features of vimclojure, making it easy to see exactly what parentheses closes which statement:
Now that VimClojure is set up, time to set up the integration with Clojure’s REPL, to do that I use slime.vim. Slime.vim uses screen to send the input from your editor to any window in a running screen session, so to get started we’ll have to start up a screen session. To make it easier, you can name it something so you don’t have to look up the pid, I’ll call this session “clojure”:
‹ ~ › : screen -S clojure
If you didn’t name your session, or forgot what you named it, you can use screen -ls to look up all the screen sessions you’ve started:
‹ ~ › : screen -ls
There are screens on:
41837.clojure (Attached)
8970.ttys000.Xanadu (Attached)
8990.ttys001.Xanadu (Attached)
9010.ttys002.Xanadu (Attached)
4 Sockets in /tmp/screens/S-hinmanm.
Now, start a REPL in the screen terminal window (use ‘clj’ or ‘lein REPL’ or however you like to start a Clojure REPL). Next, open a clojure file with Vim, highlight a block of code (slime.vim will automatically select a paragraph if your cursor is in the middle of something like a defn), now, press Control-c + Control-c (Ctrl+c twice in a row). You should be prompted by Vim like this:
Enter the name of the screen term, if you named your session “clojure” you’d enter “clojure”, if you didn’t name it, use the pid number you see from the output of ‘screen -ls’, next it will ask for which window to send the output to:
If you’ve used screen before (and I’m assuming you have), this is the window number your REPL is running on. After you enter this information the plugin will send the paragraph/line of text to the REPL. From here on the session id and window will be cached, so hitting ctrl+c,ctrl+c again will immediately send whatever function the cursor is on to the REPL. You can also select a block of code using visual mode and use ctrl+c,ctrl+c to send everything selected to the REPL. If you used the wrong numbers, use ctrl+c,v (Control+c, then v) to have slime.vim ask you for the numbers again.
There you go, you now have a 1-way pipe from your Vim editor to any kind of REPL (be it Clojure, Ruby or Python). Here’s a couple of screenshots of the plugin in action:
I know this doesn’t even come close to the amount of integration the Emacs has using SLIME, but for me, this is exactly what I want out of a Clojure development environment, develop some code and be able to easily send it to a REPL. Hopefully a Vim user or two out there will find this setup useful.
UPDATE: If you’re interested in my full Vim setup for some reason, you can check it out here.
UPDATE 2: Want to automagically fold Clojure methods when using Vim? Check out this post.
Comments
31 Responses to “How I develop Clojure with Vim”
Leave a Reply





For those interested, I have adapt slime.vim to use tmux instead of screen. You can find the plugin here: http://github.com/kikijump/tslime.vim
Hi. Your vim setup is ver nice. Which font are you using?
@C.Coutinho: cool, I was considering switching to tmux, now I have another push in that direction.
@Pedro: I use Monaco, 9pt with anti-aliasing turned off.
Thanks Lee. I tested MacVim with set noantialias and guifont=Monaco:h9 but it looks very diferent from your system.
Heh, I just spent two weeks struggling with vimclojure/nailgun (namespace issues) and slimv (annoying screen refreshing) before settling on the exact same configuration.
The only thing that’s missing for me is a keybinding to send the entire buffer to the REPL. Time to brush up on my vimscript.
@Jeff: This should work for you in your .vimrc:
nmap ggVG
Then use Ctrl+m to send the whole file.
Bah, wordpress didn’t like that comment, see it here: http://gist.github.com/333326
Hey Lee,
Fantastic blogpost, I’m glad somebody balanced the scale. You obviously have a great setup which works well for you, so I’m very happy that you decided to share!
Lau
@Lee: Worked like a charm. Thank you!
I can’t load the 3 first pictures of your article.
@JCL: That’s strange, can you get to a direct link to the image? http://writequit.org/blog/wp-content/uploads/2010/03/rainbow-paren.png
[...] 16th, 2010 How I develop Clojure with Vim : :wq – blog (tags: clojure editor vim repl) John Goulah » Node.js, Websockets, and the Twitter Gardenhose [...]
@Lee : No. But I just checked if I could load it from my home computer (through ssh) and yes, I can. It’s probably the firewall of my workplace, it is very restrictive. Now the other images don’t work either, I guess I got lucky loading them the first time.
Perhaps the firewall is blocking .png’s ? Hahahaha.
[...] already, I’ll refer you to this excellent blogpost, demonstrating an effective Vim-Clojure setup: Write Quit. Here’s what it looks like in [...]
If anyone is having namespace issues with vimclojure, and you use lein, try org.clojars.brandonw/lein-nailgun
I set it up so it basically mimics swank-clojure in starting a nailgun instance in the project’s namespace.
It takes care of getting the current stable version of vimclojure for you. Just make sure you have the same version of .vim files (2.1.2) and it should work fine.
Rainbow parens are very distractive..
[...] there’s a round up of Clojure IDEs. I stuck to Vim, but I didn’t take it as far as this guide, which describes turning Vim into a fairly comprehensive Clojure [...]
[...] How I develop Clojure with Vim : :wq – blog – March 21st %(postalicious-tags)( tags: vim programming editor clojure development tools screen ide slime )% [...]
@Lee
re: your mapping to send the entire buffer to screen
One problem with that is that your cursor ends up at the bottom of the buffer, when done. Here’s another way:
nmap :call Send_to_Screen( join(getline(1,1000000), “\n”) . “\n” )
(hope that didn’t get mangled too much)
PS: yes, I’m sure its a dumb way to do it, and look forward to anyone more well versed in vimscript correcting it
ok, seems like wordpress left out the 1st arg to nmap (the key combo). Anyway, you have the commands to bind with
Or you can just add `. to the end of the gist Lee posted, this will put the cursor on the last edited line.
[...] How I develop Clojure with Vim [...]
Lee – which color scheme is that you’re using on http://writequit.org/blog/wp-content/uploads/2010/03/gui-split.png ? [I quite like the greens and blues on that!]
- Viksit
@viksit: I use the rdark colorscheme for gvim, http://www.vim.org/scripts/script.php?script_id=1732
Is there a particular reason why you don’t like the nailgun interface?
@Simon: I’m not really sure, maybe an irrational desire for more separation?
I will say that I’ve been testing the pre-release of VimClojure (the one Meikel released on the mailing list) and actually using the nailgun/in-buffer repl features. I’m liking it so far, so I’ll probably end up sticking with that.
[...] plug-in counter-clockwise, but had some problems with it and I currently use Vim set-up according to this guide at write-quit. (I see counter-clockwise has had a new release since I tested it, so it may well be worth [...]
Just copying the vimclojure files to .vim seems like overkill. The only functionality it buys is indent and syntax highlighting. The other plugins make loading of clojure files a bit slow.
I tried just copying the indent and syntax files but indent didn’t work(there was a couple of function which I had to resolve manually; it didn’t work even after that). So finally I settled down on using nailgun. The auto-completion, doc-lookup, buffer execution etc everything works now.
Good stuff, just used this to get setup and was up and running in a matter of minutes.
[...] How I develop Clojure with Vim Weitere themenbezogene Artikel:Funding Clojure 2010Clojure – Destillat #6Clojure – Destillat #2Clojure Screencasts Teil IIClojure – Destillat #9Powered by Contextual Related Posts verlinkt mich / drucken: [...]
[...] @dnene If u r installing vimclojure, use this link http://writequit.org/blog/?p=386 .. It worked for… @dnene If u r installing vimclojure, use this link http://writequit.org/blog/?p=386 .. It worked for me .. – Debasish Ghosh (debasishg) http://twitter.com/debasishg/statuses/22734777511 (tags: from:debasishg via:packrati.us) [...]