How I develop Clojure with Vim

March 15, 2010

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.

tags: , , ,
posted in clojure, repl, screen, vim by Lee

Follow comments via the RSS Feed | Leave a comment | Trackback URL

44 Comments to "How I develop Clojure with Vim"

  1. C.Coutinho wrote:

    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

  2. Pedro wrote:

    Hi. Your vim setup is ver nice. Which font are you using?

  3. Lee wrote:

    @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.

  4. Pedro wrote:

    Thanks Lee. I tested MacVim with set noantialias and guifont=Monaco:h9 but it looks very diferent from your system.

  5. Jeff wrote:

    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.

  6. Lee wrote:

    @Jeff: This should work for you in your .vimrc:

    nmap ggVG

    Then use Ctrl+m to send the whole file.

  7. Lee wrote:

    Bah, wordpress didn’t like that comment, see it here: http://gist.github.com/333326

  8. Lau B. Jensen wrote:

    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

  9. Jeff wrote:

    @Lee: Worked like a charm. Thank you!

  10. JCL wrote:

    I can’t load the 3 first pictures of your article.

  11. Lee wrote:

    @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

  12. InVisible Blog » links for 2010-03-16 wrote:

    […] 16th, 2010 How I develop Clojure with Vim : :wq – blog (tags: clojure editor vim repl) John Goulah » Node.js, Websockets, and the Twitter Gardenhose […]

  13. JCL wrote:

    @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.

  14. Clojure IDEs?—?The Grand Tour | BEST IN CLASS wrote:

    […] already, I’ll refer you to this excellent blogpost, demonstrating an effective Vim-Clojure setup: Write Quit. Here’s what it looks like in […]

  15. Brandon wrote:

    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.

  16. anynomous wrote:

    Rainbow parens are very distractive..

  17. Clojure: 12 New Programming Languges Update 1 | Coderholic wrote:

    […] 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 […]

  18. Delicious Bookmarks for March 21st from 08:36 to 09:09 « Lâmôlabs wrote:

    […] How I develop Clojure with Vim : :wq – blog – March 21st %(postalicious-tags)( tags: vim programming editor clojure development tools screen ide slime )% […]

  19. Imran wrote:

    @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 :)

  20. Imran wrote:

    ok, seems like wordpress left out the 1st arg to nmap (the key combo). Anyway, you have the commands to bind with :)

  21. khingebjerg wrote:

    Or you can just add `. to the end of the gist Lee posted, this will put the cursor on the last edited line.

  22. Clojure – Destillat #6 | duetsch.info - Open Source, Wet-, Web-, Software wrote:

    […] How I develop Clojure with Vim […]

  23. viksit wrote:

    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

  24. Lee wrote:

    @viksit: I use the rdark colorscheme for gvim, http://www.vim.org/scripts/script.php?script_id=1732

  25. Simon wrote:

    Is there a particular reason why you don’t like the nailgun interface?

  26. Lee wrote:

    @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.

  27. Feeding the Bit Bucket» Blog Archive » Clojure Resources wrote:

    […] 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 […]

  28. Rahul wrote:

    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.

  29. Steve wrote:

    Good stuff, just used this to get setup and was up and running in a matter of minutes.

  30. Clojure – Destillat #11 | Open Source und Wetware wrote:

    […] 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: […]

  31. » links for 2010-09-01 (Dhananjay Nene) wrote:

    […] @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) […]

  32. Recommended reads for learning Clojure - Jacolyte's Lair wrote:

    […] Clojure development with Vim – Resources for setting up Vim to work beautifully with Clojure. Also, rainbow parenthesis. […]

  33. Project Euler problem #1 in Clojure - Jacolyte's Lair wrote:

    […] know vim really well, and don’t want to switch to Emacs just yet. So, I found this post and got vimset up with all kinds of clojure […]

  34. joan81 wrote:

    Now, add the settings you need for VimClojure to your .vimrc:I have to say, rainbow parentheses

  35. David25 wrote:

    Now, add the settings you need for VimClojure to your .vimrc:I have to say, rainbow parentheses

  36. pt wrote:

    Works in windows 7?

    It seems your system is mac os x or linux

  37. asa 5500 wrote:

    Thanks for the excellent post! Bookmarked! Will come back again

  38. find postal zip code wrote:

    havent played mgs2 or 3 or 1 for along time. do some research n ull find out pretty quick. Idk why snake n raiden n vamp love the spandex though.

  39. Clojure resources « Leandro Moreira wrote:

    […] 3 – VIM for Clojure […]

  40. bioknow wrote:

    +1

  41. Elliott Hauser - since 1983 wrote:

    […] VimClojure – I started out using pico, which served me well, but now mostly use vim for terminal editing.  Set up rainbow parens in vimslime as described here. […]

  42. Jonathan Marchand wrote:

    Thanks you very much! It was exactly what I was looking for, and works perfectly in tmux.

  43. Clojure Adventures | Taka Goto wrote:

    […] easy solutions.  A simple google search led me to vim plugins for easy development with clojure.  VimClojure definitely enlightened me to explorer further into clojure.  While doing the clojure kata there […]

  44. Steve Mustafa wrote:

    How has your setup been affected by the introduction of vim-clojure-static. I ask because I’m new to clojure and sometimes, getting vim to play nice can be a bit… challenging.

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org