Posts Setting Up VS Code For Clojure And Overtone Development become a patron

Setting Up VS Code For Clojure And Overtone Development

Introducing VS Code

In my previous blog post I wrote a guide on setting up Overtone and starting it on your computer. If you are interested in live coding you should definitely check it out. I concluded that article with a thought that the true power of Clojure and Overtone can be really experienced through using it with an IDE/editor. There are several of them out there that do the job well, and I am going to mention one that is popular nowdays - Visual Studio Code. The important thing is that no matter which editor/IDE you’re using you will be able to follow along this and the future articles and try out the code yourself. I chose to describe the setup process for VS Code because lots of people are already using it for their various programming needs, and I figured that it will be easier for someone to try something new if it’s somewhat familiar.

Why is this important?

Writing and executing Clojure code in an editor with integrated REPL brings us many benefits. For example, many editors with Clojure environments have Paredit enabled, which handles parenthesis balance and lets us concentrate on the code we’re writing and not on counting parenthesis. This makes things so much easier. This setup also enables us to execute (or evaluate) parts of our code in a file whenever we want it, which is very helpful for live coding. There are of course many more benefits, but we’ll take a look at them when we dive further into our journey. For now, let’s set up our editor/IDE!

VS Code + Calva

The IDE we’re going to take a look in this article is Visual Studio Code OSS with Calva development environment for Clojure. I choose to mention this combination first because of massive popularity of VS Code as a programming environment. I found it fairly easy to setup and use, so I think you won’t have any problems if you choose to use this environment. Once you install VS Code, open it and go to File -> Preferences -> Extensions or use the shortcut Ctrl+Shift+X. Search for Calva in extension marketplace and install it.
Now that we have everything set up, let’s open our Clojure project folder (it is important to load the whole directory). If you haven’t created the project and added Overtone as dependency yet, learn how to do that in my previous blog post Overtone: Basic Setup. Go to File -> Open Folder... or hit Ctrl+K Ctrl+O. If that shortcut confuses you just hold Ctrl key and then press K, release K while still holding Ctrl, and press O. This style was inspired by another editor which I’m going to describe in great detail in the next article. Now with folder selection window open, just select your Clojure Overtone project directory and click OK. Once the project loads into editor, on the left side there will be ‘Explorer’ tab with tree structure of your project. Here’s a screenshot of mine, you can see that i opened the core.clj file and it’s location within the project:

VS Code + Calva Project: VS_Code + Calva Project

Now go ahead and open your core.clj file in editor. There should be a foo function generated by Leiningen (if you created the project that way). You don’t have to change anything for now, we first need to start a REPL inside the editor. In lower left part of the screen there should be a text reading “REPL”. Go ahead and click on it and in the top center part of the screen select “Start your project with a REPL and connect (a.k.a. Jack-in)”. The dialog will ask for a project type. In my case I’ll select Leiningen and if you also used Leiningen to create your project you should select that too. Soon after, the right part of the screen will be populated by messages informing you about the process of starting a REPL. When it’s done it should look a little something like this:

VS Code + Calva REPL Loaded: VS Code + Calva REPL Loaded

Trying it out

All that is left is to test out if everything is working. First, we’ll test if we can execute the code from our file in a REPL that we loaded. Position your cursor just after the last parenthesis in the file (that would be the end of our foo function) and click Alt+Enter key combination. The REPL should respond to this action, print the name of the function you gave to it and present you with another prompt waiting for the next action. What we did here is that we loaded our function in our REPL so that we can use it. Now, if you put your cursor just after the clj꞉my-overtone-project.core꞉>  and type (foo (+ 3 5) and press Alt+Enter. If everything’s OK you should get the output 8 Hello, World!. Now, that is not very different than typing code in the terminal and executing it. The power of an editor lies in many things, one of them being that you can execute part of a code from a file. Go to core.clj file on the left and on the new line type (foo "Hello, People!), move your cursor to the end of the line (after the parenthesis) and execute the code with familiar shortcut Alt+Enter. As you can see, the result of a function call will appear in our REPL. This way enables us to write and test code in realtime, which is of great help for live coding.

Overtone in VS Code (with Calva)

Before we execute any code first we need to disable pretty-print option for REPL. This is required because Overtone isn’t perfect, and in some environments it can behave somewhat unexpectedly. In this case, for every code execution it will print several thousand lines of code. This problem is noted on github issues section of overtone here. You can avoid having this problem by clicking on “pprint” option located in lower right corner of VS Code. The text will be grey if option is disabled, or white if it’s enabled. We need it to be disabled.
Now, let’s test out Overtone with a short example. Type in your core.clj file the following code snippet:

(use '
(use 'overtone.inst.synth)

(defn play [x]
  "Can play either single tone: (play 60), (play :C4) or Whole chords"
  (if (seqable? x)
    (doseq [note x] (overpad note))
    (if (keyword? x)
      (overpad (note x))
      (overpad x))))

(play :C4)
(play (chord :C4 :minor))

Before I explain what each part of code does, remember that you need to connect Overtone audio output to your sound card, as described in my previous article.
First two line of code are loading two of the Overtone’s namespaces into our project. This should be done on top of the file using require, but use is fine for our quick test, and less complicated. In the next code block I defined a play function which uses a sound generator named overpad, and it was imported from overtone.inst.synth namespace. I chose it because it sounds pretty nice for our example. The play code block defines a function that will produce a sound when called, either of midi note, of a human-readable representation of that note, or a chord containing multiple notes. The last two lines are calling play function with some arguments, which will produce some fine results. Now, position your cursor after the parenthesis of the first line and press Alt+Enter. Your REPL should report that namespace was loaded into REPL. Move on to the next line and do the same. You just loaded overtone.inst.synth namespace into the REPL. Next, skip couple of lines and position your cursor at the end of play function definition, after the last parenthesis on (overpad x)))) line and press Alt+Enter. The play function is evaluated and we can now start producing some sounds. Position the cursor at the end of (play :C4) line and execute it (with Alt+Enter shortcut, but you probably remembered how to do that by now). What you hear is a C4 note played with Overpad instrument defined in Overtone. Now, do the same for next line and you should be hearing a richer, “sadder” tone. That’s because we’re playing the minor chord, and all minor chords in western music sound somewhat sad or melancholic. If you are to change that last line to play (chord :C4 :major)) and execute it, you will hear a happier, more positive chord. Congratulations, you’ve just took a small but significant step in your live coding journey.


We have just set up a complete environment for Clojure and Overtone programming, and you saw from trying out some small code snippets how it can be more pleasant to use than just a REPL in a terminal. I encourage you to play a little with above code snippets, change tone from :C4 to :G3 for example, and chord from :minor to :major or :major7 and see how different variations sound. For my next article I will describe another editor that is perfect for using with Clojure, as it has a mature development environment called CIDER of which Calva for VS Code is based on.

This post is licensed under CC BY 4.0 by the author.