Thursday, March 3, 2011

Using Clojure in Vim - Part 2 : accessing REPL via Nailgun

I explained how to install the Clojure plugin for Vim, Vimclojure, in a previous blog entry. Now that I'm starting to get familiar with Clojure, I thought it would be nice to be able to access REPL from Vim. To achieve this, I downloaded the Nailgun client and server. Download them from the following locations:

Starting the Nailgun server

As explained in the Vimclojure help documentation in Vim (:help vimclojure), the server has to be started with clojure and clojure-contrib jars in the classpath. For example : "java -cp clojure.jar;clojure-contrib.jar;server-2.2.0.jar vimclojure.nailgun.NGServer". Actually, in the Vimclojure plugin archive, there is a "bin" directory with two batch files: ng-server (for unix) and ng-server.bat (for Windows). These batch files allow to launch the server easily. Create a ".clojure" file in the directory where you launch the batch from, and put all the necessary jars and classpath directories in it. One per line.

After launching Nailgun, a message should appear, telling you that the server is running :
NGServer started on, port 2113

Installing the Nailgun client

Unpack the client archive in a directory of your choice. Edit your .vimrc (_vimrc) file, and add or modify the following lines :
let vimclojure#WantNailgun = 1 
let vimclojure#NailgunClient = "D:\\nailgun-0.7.1\\ng.exe"
Replace the directory with the directory where you unpacked the client.

Using REPL in Vim

Launch Vim and create a new Clojure file. For example, simple.clj. I assume that it is saved in one of the directories of the classpath used for the Nailgun server.
(ns simple)
(defn hello [] (println "Hello"))
To launch a new Vim REPL, type <LocalLeader>sr. <LocalLeader> is defined by the Vim maplocalleader variable, which is '\' if not defined. Another useful command is <LocalLeader>sR, which will start REPL in the namespace of the current buffer (it uses Clojure's "require"). In the previous example, this would start REPL in the "simple" namespace. REPL will be opened in a new window, where Clojure can be used as usual. Very convenient. Check the following image to see the previous tiny snippet in action.

There are other useful commands, which are all explained in ":help vimclojure". One of them is <LocalLeader>lw, which executes Clojure's "doc" to the word under the cursor. It's very helpful to check the documentation of a function. Also helpful is <LocalLeader>sw, which shows the source of the word under the cursor. The preview windows opened by these commands can be close via <LocalLeader>p. Check the Vimclojure's help for more.

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.