Basic Configuration

Like Emacs itself, almost every part of CIDER is configurable. The CIDER developers have tried to implement some reasonable defaults that should work for a large portion of the Clojure community, but we know that there is nothing approaching a "one size fits all" development environment and we’ve tried to create points of customization that can account for many different peoples' preferences. In this way, you should be able to make CIDER as comfortable as possible for you.

This section doesn’t describe every possible customization that CIDER offers, but here are some of the most popular.

You can see every single customizable configuration option with the command M-x customize-group RET cider.

Disable Automatic cider-mode in clojure-mode Buffers

By default, CIDER enables cider-mode in all clojure-mode buffers after it establishes the first CIDER connection. It will also add a clojure-mode hook to enable cider-mode on newly-created clojure-mode buffers. You can override this behavior, however:

(setq cider-auto-mode nil)

Prompt for Symbol Confirmation

The default here was changed in CIDER 1.0.

By default, CIDER won’t prompt you for a symbol when it executes interactive commands that require a symbol (e.g. cider-doc). Such commands operate on the symbol at point and prompt you to provide a symbol if they can’t obtain one automatically.

If you set cider-prompt-for-symbol to t, this behavior will be inverted and CIDER will always prompt you to confirm the symbol on which a command will operate. This behavior is useful, as it allows you to edit the inferred symbol, before some operation is carried out with it (and you get to see what was inferred by cider-symbol-at-point).

(setq cider-prompt-for-symbol t)
Many interactive commands that operate on the symbol at point, accept a prefix argument that flips the behavior configured via cider-prompt-for-symbol for the current command invocation.

What the Prompts Offer

When a command asks for a symbol, a namespace or an expression, what it found at point is the default: it’s shown in the prompt and RET accepts it. The minibuffer starts empty, so to change the default rather than replace it, bring it in with M-n first.

Pressing M-n again goes on to other things you’re likely to want:

  • in a symbol prompt, the name of the definition around point and that name qualified with the namespace (my.ns/my-fn)

  • in a namespace prompt, the namespace of the buffer and a namespace named at point, followed by the loaded namespaces

  • in an expression prompt, such as that of cider-read-and-eval or cider-inspect-expr, the active region and *1, the result of the last evaluation

M-p walks the history as usual. Symbols, namespaces, expressions and test selectors each have a history of their own.

Whatever you type is completed and resolved in the namespace of the buffer you called the command from, so an alias from its ns form works in the prompt too.

Control what window to use when jumping to a definition

By default the commands that jump to a definition (cider-find-var, cider-find-ns, the jumps from stacktraces and doc buffers, etc.) have the following behavior:

  • If the definition buffer is visible simply switch to it.

  • Otherwise, use the current window to show the definition.

Other behavior is possible, and is controlled with cider-jump-to-pop-to-buffer-actions; the value of this is passed as the action argument to pop-to-buffer.

Unless cider-use-xref was turned off before CIDER got loaded, M-. in a source buffer is xref-find-definitions, which picks the window on its own and doesn’t consult this option.

The default value is ((display-buffer-reuse-window display-buffer-same-window)).

Some people might prefer to always display the definition in the current window. Here’s how you can achieve this:

(setq cider-jump-to-pop-to-buffer-actions
      '((display-buffer-same-window)))
Keep in mind this might cause problems with some special buffers (e.g. test report buffers), as when you try to navigate to a definition this will clobber the special buffer.

For other possibilities, see the documentation for display-buffer.

Example 1

You jump to map in core.clj when core.clj is not being displayed in another window in the current frame.

With both the default behavior and the alternative behavior defined above, the definition of map will be shown in the current window.

Example 2

You jump to map in core.clj when core.clj is being displayed in another window in the current frame.

With the default behavior, the definition of map will be shown in the existing core.clj window; all windows will show the same buffer as before the jump, and the current window will now be the one showing core.clj.

With the alternative behavior defined above, the definition of map will be shown in the current window; you will now have two windows showing core.clj, and the existing core.clj window will be unchanged.

Control whether popup buffers get selected

Many commands show their results in a popup buffer (documentation, errors, test reports, search results, macroexpansions and so on). By default CIDER selects the popup’s window, so you can start interacting with it right away. If you’d rather stay in the buffer you’re in:

(setq cider-auto-select-buffer nil)

You can also be selective about it, by listing either the buffers to select or the ones to leave alone:

;; select only the error buffer and the test report
(setq cider-auto-select-buffer '(error test-report))
;; select everything but the doc buffer and the inspector
(setq cider-auto-select-buffer '(not doc inspector))

The buffers go by the following names:

Name Buffers

error

The error (stacktrace) buffer

test-report

The test report

doc

The documentation buffer

clojuredocs

The ClojureDocs buffer

inspector

The inspector

cheatsheet

The cheatsheet

apropos

The apropos results

xref

The function references and dependencies, and the trees of the "who" commands (e.g. cider-who-calls)

browse-ns

The namespace browser

browse-spec

The spec browser

classpath

The classpath listing

macroexpansion

The macroexpansion buffer

trace

The trace buffer and the list of traced vars

tap

The tap viewer

output

The buffer evaluation output goes to when cider-eval-output-destination is output-buffer

log-frameworks

The list of log frameworks

nrepl-info

The nREPL session and middleware listings

repl-help

The REPL’s quick reference and startup report

If you list the buffers to select, the ones a future CIDER release adds to this table won’t be selected. Use the not form if you only want to exclude a few of them.

A few buffers are not affected by cider-auto-select-buffer. The ones you have to be in to use them (the scratch buffer, the REPL history, the log buffer, the macrostep buffer) and the cider-doctor report are always selected, while the pretty-printed result buffer and the refresh log never are.

Minibuffer completion

Out-of-the box, CIDER uses the standard completing-read Emacs mechanism. While it’s not fancy it certainly gets the job done (just press TAB). There are, however, ways to improve upon the standard completion if you wish to.

icomplete

icomplete is bundled with Emacs and enhances the default minibuffer completion:

(icomplete-mode 1)

You can learn more about icomplete here.

ido

ido is also bundled with Emacs and offers more features than icomplete. If you are using ido, be sure to use both ido-everywhere and ido-completing-read+. You might also want to install flx-ido.

If you’re fine with installing a third-party package for enhanced minibuffer completion you can’t go wrong with the modern and versatile ivy.

Message Displayed on Connect

By default CIDER will display an inspirational message when a new connection is established. This behavior is configurable via cider-connection-message-fn:

;; make the message more educational
(setq cider-connection-message-fn #'cider-random-tip)
;; disable this extra message altogether
(setq cider-connection-message-fn nil)

The default messages are stored in the variable cider-words-of-inspiration that you can tweak easily yourselves:

(add-to-list 'cider-words-of-inspiration "Moar inspiration!")

Of course, it goes without saying that you can do the same with cider-tips.

You can summon an inspirational message any time with M-x cider-inspire-me (and a tip with cider-drink-a-sip).

This is probably one of the most important CIDER features. Disable those amazing messages at your own risk!

Log nREPL Communications

If you want to see all communications between CIDER and the nREPL server:

(setq nrepl-log-messages t)

CIDER will then create buffers named *nrepl-messages conn-name* for each connection.

The communication log is tremendously valuable for debugging CIDER-to-nREPL problems and we recommend you enable it when you are facing such issues.

The log is trimmed once it grows past nrepl-message-buffer-max-size characters (1,000,000 by default). The requests and the responses of an exchange share a color, so you can tell the exchanges apart; the colors are taken from the faces listed in nrepl-message-faces.

Hide Special nREPL Buffers

If you’re finding that the *nrepl-server* and *nrepl-tunnel* buffers are cluttering up your development environment, you can suppress them from appearing in some buffer switching commands like switch-to-buffer(C-x b):

(setq nrepl-hide-special-buffers t)

If you need to make the hidden buffers appear when using switch-to-buffer, type SPC after issuing the command. They are left out of list-buffers (C-x C-b) as well, since hiding a buffer comes down to starting its name with a space.

Prefer Local Resources Over Remote Resources

To prefer local resources to remote resources (tramp) when both are available:

(setq cider-prefer-local-resources t)

Translate File Paths

If you are running Clojure within a Docker image, or doing something similar (i.e. you’re cider-connect`ing to a process, and there’s a directory mapping for your source paths), you typically need to set `cider-path-translations for jump-to-definition to properly work. For instance, suppose your app is running in a docker container with your source directories mounted there as volumes. The navigation paths you’d get from nREPL will be relative to the source in the docker container rather than the correct path on your host machine. You can add translation mappings easily by setting the following (typically in .dir-locals.el):

((nil
  (cider-path-translations . (("/root/.m2" . "/Users/foo/.m2")
                              ("/src/" . "/Users/foo/projects")))))

Each entry will be interpreted as a directory entry so trailing slash is optional. Navigation to definition will attempt to translate these locations, and if they exist, navigate there rather than report that the file does not exist. In the example above, the .m2 directory is mounted at /root/.m2 and the source at /src. These translations would map these locations back to the user’s computer so that navigation to definition would work.

Using the eval pseudo-variable you can make the translation dynamic, enabling the possibility of sharing the .dir-locals.el across a team of developers with different configurations.

((nil . ((eval . (customize-set-variable 'cider-path-translations
                                         (list
                                           (cons "/src" (clojure-project-dir))
                                           (cons "/root/.m2" (concat (getenv "HOME") "/.m2"))))))))

In this example, the path /src will be translated to the correct path of your Clojure project on the host machine. And /root/.m2 to the host’s ~/.m2 folder.

You need to run lein deps (or clojure -P, etc) in the host machine in order for navigation to fully work, at least once, and then, preferably, every time your Maven dependencies change. This allows the .m2 part of cider-path-translations to be actually useful.

If you can’t or won’t do that, you can use TRAMP capabilities (which CIDER supports) instead of setting up cider-path-translations. For that, you’d typically need to set up a SSH daemon within your Docker image.

You can hide all nREPL middleware details from cider-browse-ns* and cider-apropos* commands by customizing the variable cider-filtered-namespaces-regexps. The value of this variable should be a list of regexps matching the pattern of namespaces you want to filter out.

Its default value is '("^cider.nrepl" "^refactor-nrepl" "^nrepl"), the most commonly used middleware collections/packages.

An important thing to note is that this list of regexps is passed on to the middleware without any pre-processing. So, the regexps have to be in Clojure format (with twice the number of backslashes) and not Emacs Lisp. For example, to achieve the above effect, you could also set cider-filtered-namespaces-regexps to '(".*nrepl").

To customize cider-filtered-namespaces-regexps, you could use the Emacs customize UI, with M-x customize-variable RET cider-filtered-namespaces-regexps.

An alternative is to set the variable along with the other CIDER configuration.

(setq cider-filtered-namespaces-regexps '(".*nrepl"))

Truncate long lines in special buffers

By default contents of CIDER’s special buffers such as *cider-test-report* or *cider-doc* are line truncated. You can set cider-special-mode-truncate-lines to nil to make those buffers use word wrapping instead of line truncating.

(setq cider-special-mode-truncate-lines nil)
This variable should be set before loading CIDER (which means before require-ing it or autoloading it).

nREPL Connection Hooks

CIDER provides the hooks cider-connected-hook and cider-disconnected-hook that get triggered when an nREPL connection is established or closed respectively.

Here’s how CIDER uses the first hook internally to display its famous inspirational messages on connect:

(defun cider--maybe-inspire-on-connect ()
  "Display an inspiration connection message."
  (when cider-connection-message-fn
    (message "Connected! %s" (funcall cider-connection-message-fn))))

(add-hook 'cider-connected-hook #'cider--maybe-inspire-on-connect)
There are also lower-level nrepl-connected-hook and nrepl-disconnected-hook that CIDER uses internally. Most of the time end-users would be better off using the CIDER-level hooks instead.