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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

Comments

31 Responses to “How I develop Clojure with Vim”

  1. C.Coutinho on March 15th, 2010 12:55 pm

    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 on March 15th, 2010 1:11 pm

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

  3. Lee on March 15th, 2010 1:15 pm

    @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 on March 15th, 2010 1:37 pm

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

  5. Jeff on March 15th, 2010 1:53 pm

    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 on March 15th, 2010 1:58 pm

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

    nmap ggVG

    Then use Ctrl+m to send the whole file.

  7. Lee on March 15th, 2010 1:58 pm

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

  8. Lau B. Jensen on March 15th, 2010 1:59 pm

    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 on March 15th, 2010 2:08 pm

    @Lee: Worked like a charm. Thank you!

  10. JCL on March 16th, 2010 6:44 am

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

  11. Lee on March 16th, 2010 8:55 am

    @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 on March 16th, 2010 9:01 am

    [...] 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 on March 16th, 2010 11:24 am

    @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 on March 16th, 2010 12:08 pm

    [...] 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 on March 16th, 2010 8:46 pm

    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 on March 17th, 2010 2:26 am

    Rainbow parens are very distractive..

  17. Clojure: 12 New Programming Languges Update 1 | Coderholic on March 20th, 2010 3:22 am

    [...] 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 on March 21st, 2010 6:31 am

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

  19. Imran on March 24th, 2010 5:59 pm

    @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 on March 24th, 2010 6:00 pm

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

  21. khingebjerg on March 25th, 2010 1:51 pm

    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 on March 27th, 2010 4:03 am

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

  23. viksit on March 28th, 2010 5:36 pm

    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 on March 28th, 2010 5:39 pm

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

  25. Simon on April 22nd, 2010 7:12 am

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

  26. Lee on April 22nd, 2010 9:23 am

    @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 on May 13th, 2010 1:03 pm

    [...] 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 on May 15th, 2010 11:46 pm

    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 on July 3rd, 2010 8:47 am

    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 on July 17th, 2010 1:35 am

    [...] 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) on September 1st, 2010 1:01 pm

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

Leave a Reply




Bad Behavior has blocked 195 access attempts in the last 7 days.