Skip to content

Clojure Development Overviewλ︎

A REPL workflow is highly encouraged for effective Clojure development.

Start a REPL process for a Clojure project and connect an editor to the REPL, allowing code to be evaluated and results returned instantly.

Simplest Clojure CLI project

A deps.edn file containing an empty hash-map {} is the simplest Clojure CLI project recognised by Spacemacs Clojure layer (CIDER).

The CLI install configuration includes the Clojure library as a dependency and the src directory added to the class path, so Clojure .clj files under the src directory can be used with the REPL.

Project Templatesλ︎

Create a Clojure project from a template to include common libraries and code. A template can be simple configuration with a single namespace or highly detailed and functional service.

Practicalli Project Templates provide production-ready templates to build web services and APIs upon.

Practicalli Project Templates

Emacs shell or Command Line terminal

SPC ' opens an Emacs popup buffer containing an shell terminal to run a command to create a Clojure project from a template.

Or open a terminal window from your operating system for a command line shell.

Create Project from Templateλ︎

Create a minimal project with Clojure CLI using the practicalli/minimal template and a project called practicalli/playground

Create a minimal project

clojure -T:project/create :template practicalli/minimal :name practicalli/playground

Create a new project that provides the structure of a production ready web service with example API.

Create a project for a Web Service with API

clojure -T:project/create :template practicalli/service :component donut :name practicalli/playground

Open Project filesλ︎

Use Spacemacs menu to open files from the Clojure project

Space p f lists all project files, type characters to narrow the list, Enter to select and open the file

Space p q toggles between source code namespace and its related test namespace

Space f f to navigate the file directory, Tab to complete file and directory names, Enter to open the selected file. Create a new file by typing its name and pressing return, the file and any intermediate directory path will be created

Space f D to permanently delete a file.

Hint

Clojure LSP automatically adds an ns form to define the namespace when creating a new Clojure file.

Start a REPLλ︎

Use the editor to start a REPL process, cider-jack-in. Or start a REPl in a command line terminal and connect to that process from the editor, cider-connect.

, ' (sesman-start) and select cider-jack-in-clj to start a REPL for the current Clojure project (also works without a project).

Run a rich terminal UI using the :repl/rebel alias from Practicalli Clojure-Cli-Config

The :repl/rebel alias also runs an nREPL server for an editor to connect to.

clojure -M:repl/rebel

REPL Driven Development In Spacemacs

Evaluate codeλ︎

Use the buffers containing source code files to effectively evaluate Clojure expressions.

Results are returned instantly and shown in-line with the code.

, e b (cider-eval-buffer) to evaluate the the source code file in the current buffer. Required namespaces are also loaded into the REPL.

, e f (cider-eval-defun-at-point) to evaluate the current expression from its top level (root).

, e p f as above with results pretty printed for human readability, opened in a new buffer.

Evaluate Clojure Code

Refactorλ︎

, r r (lsp-rename) to rename ns forms, def, defn and local names, updating any references to those names throughout the project.

Renaming a namespace also renames the filename along with requires that include the namespace in other namespaces

def / defn expressions a namespace contains should be removed from the running REPL to prevent inconsistencies between the code and REPL.

Remove defined names before renaming

Keep the REPL state clean by removing the name of a function or value before it is renamed , e u (cider-undef) removes the current var from the REPL (uses nREPL undef command)

Alternatively, , q r to restart the REPL after names have been changed or deleted.

Remove Evaluated Vars