23 Comments

Clojure Development with Visual Studio Code

Over the past couple of months, I have expressed much interest in learning Clojure. The problem was that no matter how motivated I felt on any given day, I didn’t seem to put in the time and effort required to actually learn it.

This was bothering me a quite a bit. After all, I had watched countless conference talks on the benefits of immutability and persistent data structures. Why couldn’t I get excited about learning Clojure?

Why Clojure is Difficult to Learn

Every time I decided I would learn Clojure and master the art of parentheses (it is a LISP after all), a pattern emerged. Every resource I tried to learn from suggested that I use Emacs as a text editor.

Don’t get me wrong, Emacs is a great tool, and there was a time when I used Spacemacs as my main text editor. Be that as it may, if you’re new to Clojure AND Emacs, you’re going to have an unpleasant experience. I was excited to dive into Clojure, but I would spend my time fixing issues I was experiencing with Emacs (definitely user error).

When I am learning a new subject, I tend to leverage the tools I already know instead of learning to use new tools along the way. Learning Emacs while also attempting to learn Clojure was too much all at once. That’s when I decided to try out Visual Studio Code (VS Code) for Clojure Development.

The rest of this post is dedicated to setting up VS Code for Clojure development. If you are a fan of Sublime Text and want to learn Clojure, I would recommend reading Greg’s post about his Clojure setup for Sublime Text instead.

Setting up VS Code

Under the assumption that you already have Clojure and Leiningen installed on your system, you will need to install the Clojure extension for VS Code. This extension can be installed from the Extensions tab in VS Code, which you can open by pressing ⇧-⌘-x on OSX or shift-ctrl-x on Windows.

Once the extension is installed, you should have everything you need! But here are a few tips to help you get started:

Connecting to the REPL

Note: As of version 0.7.0 the extension automatically connects to the REPL and pulls any necessary dependencies when you open a Clojure file. If you need to connect to the REPL manually, this option is still viable.

If you would prefer having all your tools running in a single application, VS Code has a built-in terminal emulator that uses your system’s default shell. You can use this shell to start your Clojure REPL (since this extension requires a REPL connection), or you can use your standard terminal.

To open the terminal, press ctrl+` for both OSX and Windows.

With the terminal open, you can start the REPL by typing lein repl and pressing ENTER. Now that the REPL is running, we need to connect it to VS Code. Press ⇧-⌘-p on OSX or shift-ctrl-p on Windows to bring up the list of available commands, type “Connect,” and press ENTER to select the “Clojure: Connect to nREPL” command.

This command will prompt you to enter a REPL port number and a host address as seen here:

Connecting VS Code to the running REPL using a port number and host address.

Evaluating Code with the REPL

Connecting to the REPL gives us access to the other excellent features this extension provides, such as typeahead, peek/go-to definition support, and other sorts of static-analysis. We can also take advantage of the connected REPL and evaluate portions of code without compiling the entire application.

You can see below how I highlight a section of code and evaluate it using the REPL:

Evaluate code using the REPL
Selecting a portion of code and evaluating it through the connected REPL.

Setting up Keyboard Shortcuts

The last step is to set up keybindings for evaluating our code in the REPL. VS Code has great support for setting/overriding keyboard shortcuts, which you can access through Preferences or by pressing ⌘+k ⌘+s on OSX or ctrl+k ctrl+s on Windows.

From here, search for the command “Clojure: Evaluate and show the result” and set any keybinding you want, making sure not to overwrite a keybinding you already use. As an example, I’ve set the “Clojure: Evaluate” command to ⌘+e and the “Clojure: Evaluate and show the result” command to ⇧+⌘+e.

Final Thoughts

VS Code has been my main text editor for half a year now, which has made it an excellent fit when I wanted to learn a new programming language. There are definitely better editors out there for Clojure (like Emacs), but when you’re starting out trying to learn a language, it’s important to use the tools that make you comfortable.

Now that I am more comfortable with the language, I can explore better tooling. This VS Code setup has definitely helped me bridge the gap into unfamiliar territory.

What was your experience learning Clojure for the first time?