CIDER packs a ton of extra functionality, besides basic Clojure code evaluation. Much of the functionality is centered around additional major modes, which provide you with convenient ways to get something done or inspect something.
Evaluating Clojure code in the minibuffer
You can evaluate Clojure code in the minibuffer from pretty much everywhere by
cider-read-and-eval (bound in
cider-mode buffers to
C-c M-:). TAB completion will work in the minibuffer,
just as in a REPL/source buffer.
Pressing C-c C-v . in a Clojure buffer will insert the defun at point into the minibuffer for evaluation. This way you can pass arguments to the function and evaluate it and see the result in the minibuffer.
You can also enable
eldoc-mode in the minibuffer by adding the following to your
(add-hook 'eval-expression-minibuffer-setup-hook #'eldoc-mode)
You can also enable
smartparens for minibuffer evaluations:
(add-hook 'eval-expression-minibuffer-setup-hook #'paredit-mode)
Using a scratchpad
CIDER provides a simple way to create a Clojure scratchpad via the
cider-scratch command. It provides a great way to
play around with some code, without having to create source files
or pollute the REPL buffer.
In many ways the CIDER scratchpad is similar to Emacs's own
Pressing C-c C-m after some form in a source buffer or the REPL will
result in a new buffer, showing the macroexpansion of the form in
question. You'll have access to additional keybindings in the macroexpansion
buffer (which is internally using
|g||The prior macroexpansion is performed again and the current contents of the macroexpansion buffer are replaced with the new expansion.|
|Undo the last inplace expansion performed in the macroexpansion buffer.|
Pressing C-c M-i after some form in a source buffer or the REPL will result in a new buffer, showing the structure of the result of the form in question. You can also use C-u C-c M-i to inspect the result of the current top-level form and C-u C-u C-c M-i to read an expression from the minibuffer and inspect its result.
You'll have access to additional keybindings in the inspector buffer (which is
|Tab or Shift-Tab||Navigate inspectable sub-objects|
|l||Pop to the parent object|
|g||Refresh the inspector (e.g. if viewing an atom/ref/agent)|
|SPC||Jump to next page in paginated view|
|M-SPC||Jump to previous page in paginated view|
|s||Set a new page size in paginated view|
Enlighten (display local values)
This feature displays the value of locals in realtime, as your code is being executed. This is somewhat akin to one of the features of the Light Table editor.
- To turn it on, issue M-x
- To use it, evaluate your functions one at a time (e.g., use C-M-x or C-x C-e, because C-c C-k won't work).
That's it! Once your code executes, the regular old buffer on the left will turn into the brilliant show of lights on the right.
|Enlighten Disabled||Enlighten Enabled|
To stop displaying the locals you'll have to disable
and reevaluate the definitions you had instrumented previously.
You can also trigger this on specific functions (without having to turn on the
minor mode) by writing
#light before the
(def and reevaluating it.
cider-ns-refresh will cause all modified Clojure files on the classpath
to be reloaded. You can also provide a single prefix argument to reload all
Clojure files on the classpath unconditionally, or a double prefix argument to
first clear the state of the namespace tracker before reloading.
The above three operations are analogous to
(followed by a normal refresh), respectively.
- You can define Clojure functions to be called before reloading, and after a
successful reload, when using
(setq cider-ns-refresh-before-fn "user/stop-system!" cider-ns-refresh-after-fn "user/start-system!")
These must be set to the namespace-qualified names of vars bound to functions of no arguments. The functions must be synchronous (blocking), and are expected to be side-effecting - they will always be executed serially, without retries.
By default, messages regarding the status of the in-progress reload will be displayed in the echo area after you call
cider-ns-refresh. The same information will also be recorded in the
*cider-ns-refresh-log*buffer, along with anything printed to
You can make the
*cider-ns-refresh-log*buffer display automatically after you call
cider-ns-refreshby setting the
cider-ns-refresh-show-log-buffervariable to a non-nil value (this will also prevent any related messages from also being displayed in the echo area):
(setq cider-ns-refresh-show-log-buffer t)
- By default, all modified Clojure buffers are prompted to be saved. This
behaviour can be customized using
cider-ns-refresh does not work for you the
cider-ns-reload-all commands can be used instead: they will
evaluate the Clojure's out of the box
(require ... :reload|:reload-all) at
Tracing function execution
You can trace the results produced by functions using C-c M-t v. The command will prompt you for the name of the function you want to trace. Evaluating the command again for the same function will result in the function being untraced.
You can also use C-c M-t n to toggle tracing on and off for an entire namespace.
You can easily browse the items on your classpath with the command
Here you can see it in action:
Press RET on a classpath entry to navigate into it.
You can browse the contents of any loaded namespace with the command
cider-browse-ns. The command will prompt you for the namespace
You can also browse all available namespaces with M-x
There are a bunch of useful keybindings that are defined in browser buffers.
|d||Display documentation for item at point.|
|RET||Browse ns or display documentation for item at point.|
|s||Go to definition for item at point.|
|^||Browse all namespaces.|
|n||Go to next line.|
|p||Go to previous line.|
If you are using Clojure 1.9 or newer you can browse the Clojure specs registry.
If you know what you are looking for, you can type M-x
It will prompt you for a spec name to browse to, hit RET and you will find yourself
at the spec browser.
You can also type the command M-x
cider-browse-spec-all. This command will prompt you for
a regex you can use to filter out the specs you are interested in, and will also take you to the spec browser.
Once in the browser you can use your mouse or the keybindings below to navigate deeper into sub specs.
|RET||Browse the spec at point.|
|^||Go up in the navigation stack.|
|n||Go to next spec.|
|p||Go to previous spec.|
|e||Generate an example for the current browser spec.|
If your project contains a version of
org.clojure/test.check, you can type e when browsing
a spec to generate and print an example of it.
Documentation buffers include "See Also" references
You can add references to other vars by including their names in
` in the docstring.
If the var is in another namespace, then you'll have to include the full
namespace qualified name in the docstring. If you want to use some other delimiter instead
of the backticks, you'll have to update the value of
cider-doc-xref-regexp to match that.
The first group of the regexp should always match the var name.
As an example, if you want to want to use the delimiter style used by
[[...]]) the regexp would be;
(setq cider-doc-xref-regexp "\\[\\[\\(.*?\\)\\]\\]")
Example function with a docstring containing references:
(defn test-fn "Test function. Also see: `clojure.core/map`, `clojure.core/reduce`, `defn`. You can reference variables like `thor`, `kubaru.data.zookeeper/yoda`. Also works with references to java interop forms, `java.lang.String/.length`."  (+ 1 1))