Head into Clojure – Part 4: Leiningen – A better REPL than REPL

So far we have being using the default REPL ( Read-Evalulate-Print Loop ) for entering our Clojure code and seeing how it works.

We are now going to take a look at a much better REPL, one built into another tool called Leiningen. Leiningen is much bigger than an improved REPL, it is a complete project configuration and automation tool, much like Maven or Ant, which all Clojure developers come to rely on.

Head over to leiningen.org and follow the basic instructions for installation onto your platform. We’ll touch on the power of Leiningen in further parts, but for now we are only interested in the REPL

Once you have it installed, to get a new and improve REPL, enter

lein repl

This will then load a REPL which will look something like

REPL server started on port 63316 on host 127.0.0.1 - nrepl://127.0.0.1:63316
REPL-y 0.3.5, nREPL 0.2.6
Clojure 1.6.0
Java HotSpot(TM) 64-Bit Server VM 1.7.0_71-b14
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e

user=>

The key features we are interested in are

  • Parenthesis Matching. In the REPL enter a basic form such as (+ 2 2) as you enter the final ), you will see the cursor jump back and forth highlight the most relevant bracket. Useful when you have several nested parenthesis.
  • Auto Completion. If you cannot quite remember the name of the function or macro, start by typing as much of the name you do know and then press tag. REPL will list all names which match.
  • History. Use the up and down arrow to scroll through previous entries

Some other useful commands which help your use of REPL

  • CTRL+C. If you are half way through typing something and realise you don’t need it, rather than hit return and have REPL through an error trying to parse what you typed, use CTRL+C instead to abandon the current line.
  • CTRL+L. Once you’ve used REPL for some time you find yourself at the bottom of the screen and things start to look cluttered. CTRL+L will clear the current screen and leave you back at the top.