This post is from my friend and colleague Tim Pote. He’s a really smart guy and wrote up a tutorial on getting started with Clojure and IntelliJ/Cursive. Graciously, he’s allowed me to cross post it here. I’m currently pushing him to write more about Clojure on his blog.
The focus is Clojure, but IntelliJ/Cursive works just as well with ClojureScript (I use it and love it). In the future, I’ll add some notes on integrating tools like Figwheel.
Getting started with Clojure
One thing I often hear from people getting started with clojure is:
I’m learning clojure, and, since it seems to be the defacto standard for Lisp, I’m also learning Emacs.
Now, I’ll just come out and say it: I’m a Vim guy.
When I started learning clojure in 2012, the options were pretty much: use emacs, or use vim-fireplace. vim-fireplace is great, but it doesn’t provide you near the power and flexibility of a true REPL. Additionally, I don’t find paredit emulators in vim to be a fluid editing experience.
So, like many, I tried to learn clojure and Emacs at the same time.
I found that experience to be extremely frustrating. Not only was I having to learn and apply new language concepts, but I constantly felt hampered by my inability to edit text efficiently. Not to mention getting set up with any of the aforementioned tools is non-trivial for a newcomer. To this day, I remember the difficulty trying to “just fire up and connect to nREPL”.
Nowadays most of that pain has been alleviated by Cursive. That’s especially true if you’re accustomed to using an IDE. You get “simple” things like autocompletion, documentation, suggestions, and refactoring, all without having to “fire up a nREPL”. You get a full-fledged, interactive REPL. And you get to choose what editing mode you prefer. Emacs, Vim, point-and-click, whatever. Cursive’s got you covered.
So, now when people tell me “I’m learning clojure,” I usually respond:
Use Cursive, unless you already know Emacs
The problem with saying that is there’s still a pretty lengthy install process for an absolute beginner.
Therefore, I decided to put together a little tutorial for clojure newcomers that I can point to when the question comes up.
- Go to the Java Download Page
- Accept the license agreement
- Select the appropriate install
- Note the install location. Example install locations are:
- Linux: I have no idea. Figure it out. You asked for this :)
Leiningen is currently the defacto standard for clojure build tools. It’s used for lots of things, but in this example we’re only going to use it to generate our project.
If you’re on Windows, you should be able to use the windows installer.
If you’re on Mac/Linux, open a terminal and run:
At this point, you can start a repl with
lein repl. Yay!
Create a new clojure project
You should now be able to create your first clojure project. Simply open a terminal window, navigate to your projects directory, and run:
lein new app hello-clojure
This creates a new directory called
hello-clojure which contains the scaffolding for an “app” project. If you want to create a different kind of application, simply replace
app with the lein template of your choice. For example, if you want to create a webapp, you might choose chestnut.
Download and install IntelliJ. The Free Community Edition will suffice.
Follow the Getting Started tutorial to install the Cursive plugin.
After you’ve installed the plugin, you’re going to want to set up keybindings for Cursive. Keep in mind that IntelliJ does not allow you to overwrite the template keybindings, so you must first make a copy of your preferred keymap.
NOTE: During the project import process, you will be prompted to choose an SDK.
- Click the + button to create a new SDK
- Select JDK from the Add New SDK drop down
- Navigate to the Java install location you noted during step 1.
You can later access and modify the SDKs you have installed by going to File → Project Structure → SDKs. You can change the SDK for a particular project by going to File → Project Structure → Project and selecting a new Project SDK.
(Optional) Install IdeaVim
Lastly, we’ll mix in all the goodness of vim with the IdeaVim plugin. Go to Settings → Plugins → Install JetBrains plugin → IdeaVim. Click Install Plugin and restart.
There are a few gotchas with this plugin, but the one that gets me the most is the ability to use
Esc from the REPL. One of the IntelliJ defaults is
Esc returns you to the editor. This means that if you’re in the REPL, you have to hit
Ctrl-c if you want to enter navigation mode. You can remap
Esc but then you lose the ability to jump back to the editor altogether.
And that’s it! You now have the JVM and Leiningen installed, know how to set up a new project, and have a powerful editor.
Feel free to hit me up on twitter if there are any corrections or clarifications that need to be made to this tutorial.
Find this post useful? Share the love! Tweet