Clojure Development Workflow with Spacemacs and CIDER

This is a follow-up to my previous post What’s the Best Clojure IDE? I’ll show you my usual Clojure development workflow using Spacemacs+CIDER as primary tools.

I’ve already described my tour through the various Clojure IDEs/editors and why I’ve chosen the Spacemacs. If you haven’t read the previous post, you may want to go back and read at least the Spacemacs to the rescue part.

In this post, I’ll try to dig deeper and show you the complete development workflow. I’ve chosen video screencast as a primary format for this because I think it’s more useful to see everything in action.

We’ll create a simple clojure-repl project containing the jmethods function for listing all public methods of java classes.

You can find the video in the Curious Programmer YouTube channel.

Below is a brief summary of topics/tips presented in the screencast.

Summary

  • Generate new leiningen project: lein new clojure-repl
  • Open Spacemacs
  • Create new layout: SPC l 2; enter layout name: clojure-repl
  • Open clojure-repl/project.clj by pressing SPC f f and looking up the file on a file system
  • Open Neotree buffer via SPC p t to explore project structure
    • You can navigate in this buffer via standard vim key bindings: j, k, l, h. You can also press ? to open the help
  • Clean the initial project skeleton and commit them via magit: SPC g s
    • Expand/collapse the changes by pressing TAB
    • Stage the changes for commit by pressing s
    • Commit the staged changes by c c
  • Create java.clj file in the Neotree buffer by pressing c (“create”) and entering the name of the file.
  • Run cider-jack-in by , '
  • Switch between REPL and java.clj buffer using C-c C-z
  • Evaluate the java.clj buffer via , e b
  • Evaluate the function at a point by , e f
  • Send results from java.clj buffer to the REPL using , s e while keeping cursor right after the form you want to evaluate
  • Use clojure.core/bean function to examine the properties of java class
  • Reformat a buffer with , = to make sure that your code is compliant with Clojure style guide
  • Use SPC k s for “forward slurp”
  • Use cljr-add-missing-libspec , r a m to add a require unknown symbols (e.g. clojure.string/join) automatically
  • Show documentation popup with , h h
  • Navigate to source code by , g g

Bonus

Additional tips mentioned at the end of the video:

  • Describe key bindings via SPC ?
  • Start with M-x and type in the “cider-” and/or use menu bar (F10) to search for CIDER commands and explore CIDER capabilities
  • Add new dependencies to project.clj and hot-load them into the REPL via , r a p
  • cider-inspect-last-result
    • press ENTER to deep dive
    • press C-z to switch into the Emacs mode and then l to go up
  • Use SPC / for a quick search within the project
    • You can also use C-c C-e to enter the edit mode and replace the text
  • Type * for a quick search/highlight of a symbol under the cursor
  • Display online java doc for Java class with , h j
  • Add Clojure sources to the project.clj/profiles.clj to be able to navigate to clojure.core java classes, e.g. clojure.lang.PersistentVector
  • Not covered in video
    • cider-browse-ns , h n to list all the functions in given namespace together with a documentation preview
    • cider-refresh  , s x to test everything in a fresh environment
    • CIDER debugger – debug function at the point: , d b

Resources