Configuring Eclipse for Clojure
In preparation of the upcoming
Eclipse Demo Camp in Bonn
recently played around with Counterclockwise
and Eclipse Luna settings to enable a workflow within
Eclipse that is similar to what I'm used to in Emacs.
It's really not too complicated if you have an idea what you
want to have at the end, because CCW already provides almost
everything out-of-the-box. This is my wishlist:
- Tight integration with a REPL: I want to quickly copy code
between source files and the REPL, switch current REPL
namespace from within the editor, recompile selected
s-expressions, toplevel sexps or the whole editor
- Structural editing a.k.a Paredit, which means my edit
commands target s-expressions surrounded by parentheses or other
types of brackets, not lines of code. I want to slurp, barf,
split, join and raise based on s-expressions.
- Switch between REPL and editor with one shortcut, and copy
code between them.
- Auto-completion on demand.
- Quick execution of unit tests.
- I like to have the source on the left and the REPL on
right. But I need to change this to a top-bottom layout in
case where I need to see a web browser or other window
- Use a dark color theme to give my eyes a rest.
To summarize, I was able to get everything on this list, only
unit test execution is more cumbersome in Eclipse. Here's a
Looks pretty cool compared to standard Eclipse, eh?
Follow the instructions
To get the software in place,
Now after installation and Eclipse restart, you need to change
some preferences. Go to Eclipse Preferences:
- In General / Appearance: Select the theme "Moonrise (standalone)".
- Under Appearance go to Color Theme and select Oblivion, or any other dark theme you like.
- Within the preferences Clojure tree, select brighter REPL colors under Colors and Fonts.
- In Clojure / Editor switch Start editors in strict/paredit mode ON, and Auto-activate Code Completion OFF.
- In Clojure / REPL View switch Show Hints OFF.
- In General / Editors / Text editors set the Selection background color to a brighter value.
That's it. You're ready to play.
Starting a project and getting into the flow
You can simply type
, enter "Clojure" and
select the "Clojure Project" option. Enter the project name, for
example "helloworld". The dialog also allows you to select a
Leiningen template, but you can ignore that. Open the source
file in src/helloworld/core.clj. Type
to start the REPL. (If you're Mac user please see shortcuts
below.) Drag the REPL view to the top-right and make the right
side wide enough.
Type in the REPL input area
. You should see the output
appearing in the upper part of the REPL view. Switch to the
, change the
expression, and hit right there
recompile the foo function. You should see
appearing in the REPL output
Now, switch back to the REPL with
the last expression with
to evaluate the expression. The REPL output should reflect your
Keep your hands on the keyboard, please!
I know, programming is about thinking, not typing, but
using the REPL is a crucial part of the Clojure workflow.
You'll have to learn to control your tools solely by keyboard,
otherwise exploration of ideas will get so time-consuming that
you tend to give up too early instead of finding the best way.
Here are the shortcuts you should really train yourself:
|Movement between views / tabs|
|Change active view|
|REPL interaction from editor|
|Load whole editor contents|
|Switch REPL to files namespace|
|Eval (toplevel or selected) sexp|
|In the REPL|
|Recall previous command in REPL history|
|Recall next command in REPL history|
|Expand selection to next sexp|
|Expand selection to previous sexp|
|Expand selection to enclosing sexp|
|Raise selected sexp|
|Reindent selected sexp|
|Jump before toplevel sexp|
|Jump after toplevel sexp|
|Jump to matching bracket|
|Mini lein command line|
To learn more about existing shortcuts go to Eclipse
Preferences / General / Keys and type in "Clojure" as filter
text. This should give you a list of all Clojure relevant
Unit test execution
I wasn't able to find any clojure.test integration in
Eclipse. Which brings us back to the REPL. If you're about to
create new tests then you can certainly use
the test namespace. In order to execute tests as a regression
for a specific namespace, e.g.
wherever you are, you could issue a command like this:
(do (require 'clojure.test 'helloworld.core-test)
Since this is a little awkward to type in, you can help yourself with an
injection in your ~/.lein/profiles.clj file as demonstrated in this
This allows you to use
and through the REPL command history you can quickly re-execute
it anytime. Or you put a similar injection into your
that already "knows" all test namespaces.
Eclipse is originally a Java IDE, which brings its
plethora of tools, views and stuff that tends to distract you
from the code. Therefore you need to rearrange the perspective a
bit to bring the REPL and your code into the focus again. Emacs
with the right setup allows for an almost clutter-free look,
which I prefer over Eclipse. Emacs also excels when it comes to
keyboard control (basically it offers no other reasonable
option). But after all: if you trained yourself to use the
keyboard in Eclipse as much as possible then you will experience
the same flow with Eclipse that I have with Emacs.