Archive

Posts Tagged ‘vim’

VimClojure – Easy

October 12th, 2011 3 comments

Update 1/2013: The future of Clojuring with Vim is tpope’s foreplay.vim. You can find a tutorial here.

I promise this is the last VimClojure post for a while.

I get the feeling that my post describing my VimClojure setup is a little too involved, especially for someone that just wants something working quickly. Also, from what I’ve seen on #clojure, there’s some frustration out there. With that in mind, I put together a minimal example configuration with instructions.

So, without further ado, vimclojure-easy.

Feedback welcome.

Categories: clojure, vim Tags: , ,

Taming VimClojure 2.3.0

October 7th, 2011 No comments

Update 1/2013: The future of Clojuring with Vim is tpope’s foreplay.vim. You can find a tutorial here.

In honor of the release of VimClojure 2.3.0, I’ve updated my post on taming VimClojure. Thankfully, very little has changed in the setup. Cheers.

Categories: clojure, vim Tags: , ,

Here Comes Clojure: A Clojure Talk in Clojure

April 6th, 2011 5 comments

TL;DR: I did a talk on Clojure and wrote the slides in Clojure. Code here.

Edit: Fixed apostrophes. That’s embarrassing.

On kinda short notice I found out I was giving a talk on Clojure at this week’s CraftsmanGuild meeting in Ann Arbor. I’m not an expert, but “in the land of the blind, the one-eyed man is king” or something, so I was happy to do it. CraftsmanGuild is low pressure and fun.

So, I fired up PowerPoint, wrote a title slide, and then stared at the screen for a bit. This is no fun. Then it was the kids’ bedtime, which means I got to sit in my daughter’s room for a bit and just ponder. Like Hammock Driven Development, I guess. And as I sat there, in the glow of the nightlight, I got it: Why not implement my talk in Clojure, driven by some simple vim macros? It’s either brilliant, or really stupid.

If anything, the benefits outweighed the risks:

  • Practice Clojure while preparing my talk
  • Practice Vim while preparing my talk
  • No PowerPoint

The main risk was totally bombing in front of a friendly audience that’s just there to learn and have fun. Not too scary.

Here’s a screenshot of the title “slide”:

The implementation is super simple. Basically just a vector of strings and a current atom which points at the current slide. The advance! function moves the pointer, formats the slide (nice ascii borders and padding) and prints it.  It’s super simple, and can certainly be written better. I ran all of this in a VimClojure REPL set up as described in a previous post. There were a few cool things about this:

  • I got syntax highlighting of sample code (and admittedly, the rest of the text) for free.
  • I could immediately type examples or elaborate ideas directly in the REPL without leaving my “slides”.
  • I could evaluate code samples right from the slide and expand on them live.

I hacked (I mean they’re really kludgy) together some simple vim macros to:

  • Reload the presentation code
  • Go back to the beginning
  • Advance to the next slide
  • Evaluate the form under the cursor in the REPL. This was slightly tricky because it had to strip out the slide “borders” first.

Overall, I think the effect was pretty cool. Everything ran well, with the major problem being that the projector’s red wasn’t working so it was hard to see some of the text. Oh, and at one point I accidentally hit my caps lock with the usual confusion that accompanies it. Bygones. The actual content was probably the weakest part of the talk, but not terrible. Passable for two nights prep.

Here’s one last screenshot showing some syntax highlighting and simple ascii diagramming:

The code for the talk is all up on github: https://github.com/daveray/here-comes-clojure. Note that vim isn’t required. It will run in a normal REPL, it just won’t look as nice. And, of course, I’m sure something much more impressive is already built-in to Emacs :)

Categories: clojure, vim Tags: , ,

How I Tamed VimClojure

October 24th, 2010 15 comments

Update 1/2013: The future of Clojuring with Vim is tpope’s foreplay.vim. You can find a tutorial here.

NOTE: I really love the setup described below. But I admit it’s a lot to digest. If you just want vimclojure working now, you may be interested in vimclojure-easy


UPDATED October 7th, 2011 for VimClojure 2.3.0

TL;DR VimClojure Download List


Introduction

Like Windows users at a Ruby conference, vim-using Clojurians can feel a little left out. Everyone uses Emacs. Well, I played a ton of Rogue when I was 11, so I’ve always had a soft spot for vi/vim’s way of doing things, at least the movement keys anyway.

Up until now I’ve been doing Clojure development in a cobbled together mix of screen.vim and VimClojure. I could start a simple repl with screen and rely on VimClojure for basic syntax highlighting, indenting, etc. I’ve been aware that VimClojure had much more to offer, but the setup docs I found were varied, inconsistent and generally confusing. Other blogposts on the subject seemed to either surrender to a half solution like I already had, or were so inconvenient that I wouldn’t use it. So, I finally sucked it up, slogged through the setup, and got everything working to my liking.

Here, I’ll try to clearly document my full setup in hopes that someone else might find it useful. My main goal was a setup where I could make the most of VimClojure directly from vim without a bunch of preparation. If I want to just play around, I don’t want to fiddle around with Maven or Leiningen or anything. I want to start vim, get a repl, and start exploring!

Everything here is in my vimfiles repo on github.com

screenshot

Prerequisites

Note that throughout this document, if you’re on Windows, replace references to the ~/.vim directory with ~/vimfiles

Pathogen

If you’re not interested in using pathogen, just adjust paths accordingly throughout the rest of this post

To get going, you’ll need to download a few things. First, for vim plugin sanity, I use pathogen.vim. Download it and add pathogen.vim to ~/.vim/autoload and add the following to the top of your .vimrc file:

" Load plugins from .vim/bundles using .vim/autoload/pathogen.vim
call pathogen#runtime_append_all_bundles()

filetype off " On some Linux systems, this is necessary to make sure pathogen
             " picks up ftdetect directories in plugins! :(
syntax on
filetype plugin indent on

Now add a ~/.vim/bundle directory. This is where you’ll expand all your plugins, each in its own directory.

screen.vim

For this setup, I’m still using screen.vim as a simple way of starting up VimClojure’s Nailgun server directly from vim. It makes sure the server is killed when vim exits and is generally useful anyway. Download and install it. Since I’m not a fan of vimball (vba) installers, I just grabbed the source directly from github (hit the “Downloads” button) and dropped it in my bundle directory.

That’s it. Now you can use :ScreenShell args ... to start a simple screen session.

Note to Windows users: Obviously, Windows doesn’t have GNU screen. The least painful approach seems to be to install Cygwin. I don’t care that much for Cygwin, mostly because it has no uninstaller (wtf !?!?), but a super-minimal Cygwin install seems to be working ok for me. Just be sure to add c:\cygwin\bin (or whatever) to your system path.

VimClojure Setup

Install

And of course you need VimClojure itself. Everything here assumes VimClojure 2.3.0. Hopefully, it doesn’t change too much. Download VimClojure and extract it to your ~/.vim/bundle folder. For full interactive support, you’ll need two other things:

  • The Nailgun client (note this is 2.2.0, but still works fine with 2.3.0) – A tiny executable that allows vim to communicate with a running JVM. (as a side-note, Nailgun is an occasionally useful way to avoid the horrendous startup times of the JVM)
  • The VimClojure Nailgun server jar (you can browse here for other versions) – The other half of the Nailgun protocol where the running instance of Clojure will live in a JVM. Note that as documented in the README.markdown file in the VimClojure archive, you can also specify this as a dependency in Leiningen, Maven and friends. This isn’t that useful for ad hoc Clojure exploration though.

Assuming you’ve installed VimClojure to ~/.vim/bundle/vimclojure-2.3.0, here’s what I did with these. I created a folder, ~/.vim/bundle/vimclojure-2.3.0/lib and added the following to it:

bundle/vimclojure-2.3.0/lib
|-- nailgun
|   |-- Makefile
|   |-- ng
|   |-- ng.exe
|   |-- ngclient
|   |   `-- ng.c
|   `-- readme.txt
`-- server-2.3.0.jar

Note that the ng executable on Unix must be built with make.

Now the only files we still need are the clojure core and contrib jars. I drop these in ~/.vim/lib. Which version you put there is up to you. We’ll set things up in .vimrc below so that this is the last resort for finding Clojure jars. If a different version is in your project, that should get used instead. If you just start up vim to experiment outside a full project, this is the version you’ll get.

.vimrc Settings

Ok, we have everything we need in our .vim folder. Now we just need to wire it all up in .vimrc. First, let’s do some stuff to make things cross-platform:

" Let's remember some things, like where the .vim folder is.
if has("win32") || has("win64")
    let windows=1
    let vimfiles=$HOME . "/vimfiles"
    let sep=";"
else
    let windows=0
    let vimfiles=$HOME . "/.vim"
    let sep=":"
endif

Now we know where the .vim folder is and the correct Java classpath separator (stupid!) to use. So we can build a generic classpath that will cover most project configurations. I tend to always open vim at the root of a project and never change working directory so your mileage may vary with this:

let classpath = join(
   \[".", 
   \ "src", "src/main/clojure", "src/main/resources", 
   \ "test", "src/test/clojure", "src/test/resources",
   \ "classes", "target/classes",
   \ "lib/*", "lib/dev/*", 
   \ "bin", 
   \ vimfiles."/lib/*"
   \], 
   \ sep)

Basically we’ve just built a big, catchall classpath. Note that the jars in .vim are at the very end so they’ll be a fallback.

Finally, we’ll pull it all together by configuring VimClojure:

" Settings for VimClojure
let vimclojureRoot = vimfiles."/bundle/vimclojure-2.3.0"
let vimclojure#HighlightBuiltins=1
let vimclojure#HighlightContrib=1
let vimclojure#DynamicHighlighting=1
let vimclojure#ParenRainbow=1
let vimclojure#WantNailgun = 1
let vimclojure#NailgunClient = vimclojureRoot."/lib/nailgun/ng"
if windows
    " In stupid windows, no forward slashes, and tack on .exe
    let vimclojure#NailgunClient = substitute(vimclojure#NailgunClient, "/", "\\", "g") . ".exe"
endif

" Start vimclojure nailgun server (uses screen.vim to manage lifetime)
nmap <silent> <Leader>sc :execute "ScreenShell java -cp \"" . classpath . sep . vimclojureRoot . "/lib/*" . "\" vimclojure.nailgun.NGServer 127.0.0.1" <cr>
" Start a generic Clojure repl (uses screen.vim)
nmap <silent> <Leader>sC :execute "ScreenShell java -cp \"" . classpath . "\" clojure.main" <cr>

Basically we tell VimClojure where our Nailgun client is (and handle Windows silliness) and set up some helpers for starting a Clojure repl (<Leader>sC) or VimClojure Nailgun server (<Leader>sc) with screen.vim. If you don’t feel like using the screen shortcut, you can of course start the server manually as described in the VimClojure docs:

$ java -cp "classpath including clojure jars and server-2.3.0.jar" vimclojure.nailgun.NGServer 127.0.0.1

Kick the Tires

With all that in place, we’re ready to give it a try:

  • Start vim
  • Hit <Leader>sc (or whatever you mapped the server to) to start up the Nailgun server. Note that it must be running before you open a Clojure buffer!
  • Open a new Clojure file: :e test.clj
  • At this point, there might be a slight, or not so slight, delay (or error message!) as the Nailgun server loads Clojure for the first time. Remember that JVM startup time I mentioned?
  • Hit <LocalLeader>sr to start a repl inside vim (see :help maplocalleader if you don’t know what <LocalLeader> is)

All the other commands and interactive features of VimClojure are described in the docs (~/.vim/bundle/vimclojure-2.3.0/doc/clojure.txt). Here’s my current shortlist:

  • <LocalLeader>sw – Show source for the word under the cursor, including clojure.core! <– this is key if you care about understanding a platform!
  • <LocalLeader>sr – Start a repl
  • <LocalLeader>sR – Start a repl in the current namespace
  • <LocalLeader>eb – Evaluate current visual block. There are several “eX” variations.
  • <LocalLeader>lw – Lookup docs for word under cursor. There are several “lX” lookup variations

Conclusion

Now we have a Clojure development environment that’s not so embarrassingly inadequate compared to Emacs. This is working well for me so far, but I’m sure there’s room for improvment. Let me know if you see anything really dumb I’m doing. Thanks!

Categories: clojure, vim Tags: ,

A Brief Note On Pathogen For Vim

October 12th, 2010 4 comments

I spent a while tracking down a vim issue this evening. For plugin-management purposes, I’ve recently switched to the excellent Pathogen plugin. Right at the top of that page it says:

Add this to your vimrc:

     call pathogen#runtime_append_all_bundles()

That seems easy enough, and everything worked great on Windows. Moving over to Linux, things weren’t going as well. Whenever I opened a Clojure file, its filetype wasn’t detected. I had to manually execute “set filetype=clojure” to get syntax highlighting and even then, indenting was weird.

So, I debugged. One great thing I learned along the way is how to enable vim’s logging for debugging purposes. Just do something like this:

   $ gvim -V9log.txt ...

that will log everything vim does to log.txt in the current directory. I was able to compare my Windows log to the on Linux and see that when it searched for ftdetect plugin directories, it wasn’t including any of the plugins managed by Pathogen. hmmm… I googled it for myself and was led back to … that’s right, the same Pathogen page, down toward the bottom where it says:

Note that you need to invoke the pathogen functions before invoking “filetype plugin indent on” if you want it to load ftdetect files. On Debian (and probably other distros), the system vimrc does this early on, so you actually need to “filetype off” before “filetype plugin indent on” to force reloading.

My evening would have been funner if the top of the page just said this in the first place:

Add this to your vimrc:

     call pathogen#runtime_append_all_bundles()
     filetype off
     syntax on
     filetype plugin indent on

Yes, I should have read all the instructions. Everything’s up on github for the curious.

Categories: vim Tags: