Traditional programming languages and development environments often use a Edit, Compile, Run Cycle. In this environment, the programmer modifies the code, compiles it, and then runs it to see if it does what she wants. The program is then terminated, and the programmer goes back to editing the program further. This cycle is repeated over and over until the program behavior conforms to what the programmer desires. While modern IDEs have optimized this process to be quick and relatively painless, it's still a slow way to work.
Clojure and CIDER offer a better way to work called interactive programming. Indeed, this idea is at the very heart of CIDER.
Using CIDER's interactive programming environment, a programmer works in a very dynamic and incremental manner. Instead of repeatedly editing, compiling, and restarting an application, the programmer starts the application once and then adds and updates individual Clojure defintions as the program continues to run. Using the CIDER REPL, the programmer can access the value of different definitions and invoke program functions with test data, immediately seeing the result. This methodology is far more efficient than the typical Edit, Compile, and Run Cycle because the program continues to run and keeps its state intact while the programmer interacts with it. Indeed, some Clojure programmers have been known to keep a CIDER session running for weeks or even months as they continue to write code.
CIDER's interactive programming environment is partially implemented
using an Emacs minor mode called cider-mode
. cider-mode
complements clojure-mode
and allows you to evaluate Clojure code
from your source file buffers and send it directly to your running
program through the CIDER REPL. Using the functions offered by
cider-mode
will improve your productivity and make you a more
efficient Clojure programmer.
Using cider-mode
Here's a list of cider-mode
's keybindings:
Command | Keyboard shortcut | Description |
---|---|---|
cider-eval-last-sexp |
C-x C-e C-c C-e |
Evaluate the form preceding point and display the result in the echo area and/or in an buffer overlay (according to cider-use-overlays ). If invoked with a prefix argument, insert the result into the current buffer. |
cider-eval-last-sexp-and-replace |
C-c C-v w | Evaluate the form preceding point and replace it with its result. |
cider-eval-last-sexp-to-repl |
C-c M-e | Evaluate the form preceding point and output it result to the REPL buffer. If invoked with a prefix argument, takes you to the REPL buffer after being invoked. |
cider-insert-last-sexp-in-repl |
C-c M-p | Load the form preceding point in the REPL buffer. |
cider-pprint-eval-last-sexp |
C-c C-v C-f e | Evaluate the form preceding point and pretty-print the result in a popup buffer. If invoked with a prefix argument, insert the result into the current buffer as a comment. |
cider-pprint-eval-defun-at-point |
C-c C-v C-f d | Evaluate the top level form under point and pretty-print the result in a popup buffer. If invoked with a prefix argument, insert the result into the current buffer as a comment. |
cider-eval-defun-at-point |
C-M-x C-c C-c |
Evaluate the top level form under point and display the result in the echo area. |
cider-eval-sexp-at-point |
C-c C-v v | Evaluate the form around point. |
cider-eval-defun-at-point |
C-u C-M-x C-u C-c C-c |
Debug the top level form under point and walk through its evaluation |
cider-eval-defun-up-to-point |
C-c C-v z | Evaluate the preceding top-level form up to the point. |
cider-eval-region |
C-c C-v r | Evaluate the region and display the result in the echo area. |
cider-interrupt |
C-c C-b | Interrupt any pending evaluations. |
cider-macroexpand-1 |
C-c C-m | Invoke macroexpand-1 on the form at point and display the result in a macroexpansion buffer. If invoked with a prefix argument, macroexpand is used instead of macroexpand-1 . |
cider-macroexpand-all |
C-c M-m | Invoke clojure.walk/macroexpand-all on the form at point and display the result in a macroexpansion buffer. |
cider-eval-ns-form |
C-c C-v n | Eval the ns form. |
cider-repl-set-ns |
C-c M-n (M-)n | Switch the namespace of the REPL buffer to the namespace of the current buffer. |
cider-switch-to-repl-buffer |
C-c C-z | Switch to the relevant REPL buffer. Use a prefix argument to change the namespace of the REPL buffer to match the currently visited source file. |
cider-switch-to-repl-buffer |
C-u C-u C-c C-z | Switch to the REPL buffer based on a user prompt for a directory. |
cider-load-buffer-and-switch-to-repl-buffer |
C-c M-z | Load (eval) the current buffer and switch to the relevant REPL buffer. Use a prefix argument to change the namespace of the REPL buffer to match the currently visited source file. |
cider-describe-connection |
C-c M-d | Display default REPL connection details, including project directory name, buffer namespace, host and port. |
cider-find-and-clear-repl-output |
C-c C-o | Clear the last output in the REPL buffer. With a prefix argument it will clear the entire REPL buffer, leaving only a prompt. Useful if you're running the REPL buffer in a side by side buffer. |
cider-load-buffer |
C-c C-k | Load (eval) the current buffer. |
cider-load-file |
C-c C-l | Load (eval) a Clojure file. |
cider-load-all-files |
C-c C-M-l | Load (eval) all Clojure files below a directory. |
cider-ns-refresh |
C-c M-n (M-)r | Reload all modified files on the classpath. If invoked with a prefix argument, reload all files on the classpath. If invoked with a double prefix argument, clear the state of the namespace tracker before reloading. |
cider-doc |
C-c C-d d C-c C-d C-d |
Display doc string for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
cider-javadoc |
C-c C-d j C-c C-d C-j |
Display JavaDoc (in your default browser) for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
cider-grimoire |
C-c C-d r C-c C-d C-r |
Lookup symbol in Grimoire. |
cider-grimoire-web |
C-c C-d w C-c C-d C-w |
Open the grimoire documentation for symbol in a web browser. |
cider-apropos |
C-c C-d a C-c C-d C-a |
Apropos search for functions/vars. |
cider-apropos-documentation |
C-c C-d f C-c C-d C-f |
Apropos search for documentation. |
cider-apropos-documentation-select |
C-c C-d e C-c C-d C-e |
Apropos search for documentation & select. |
cider-inspect |
C-c M-i | Inspect expression. Will act on expression at point if present. |
cider-toggle-trace-var |
C-c M-t v | Toggle var tracing. |
cider-toggle-trace-ns |
C-c M-t n | Toggle namespace tracing. |
cider-undef |
C-c C-u | Undefine a symbol. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
cider-test-run-test |
C-c C-t t C-c C-t C-t |
Run test at point. |
cider-test-rerun-test |
C-c C-t g C-c C-t C-g |
Re-run the last test you ran. |
cider-test-run-ns-tests |
C-c C-t n C-c C-t C-n |
Run tests for current namespace. |
cider-test-run-loaded-tests |
C-c C-t l C-c C-t C-l |
Run tests for all loaded namespaces. |
cider-test-run-project-tests |
C-c C-t p C-c C-t C-p |
Run tests for all project namespaces. This loads the additional namespaces. |
cider-test-rerun-failed-tests |
C-c C-t r C-c C-t C-r |
Re-run test failures/errors. |
cider-test-show-report |
C-c C-t b C-c C-t C-b |
Show the test report buffer. |
cider-find-var |
M-. | Jump to the definition of a symbol. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
cider-find-dwim-at-mouse |
mouse-5 or mouse-9 | Jump to the definition of a symbol using mouse. |
xref-pop-marker-stack |
mouse-4 or mouse-8 | Jump back to where cider-find-dwim-at-mouse was invoked. |
cider-find-resource |
C-c M-. | Jump to the resource referenced by the string at point. |
cider-find-ns |
C-c C-. | Jump to some namespace on the classpath. |
cider-pop-back |
M-, | Return to your pre-jump location. |
complete-symbol |
M-TAB | Complete the symbol at point. |
cider-quit |
C-c C-q | Quit the current nREPL connection. |
Tip
There's no need to memorize this list. If you're in a Clojure buffer with cider-mode
active you'll have a CIDER menu available. The menu lists all the most important
commands and their keybindings. You can also invoke C-h f RET cider-mode
to
get a list of the keybindings for cider-mode
.
Tip
An even better solution would be to install which-key, which will automatically show you a list of available keybindings as you start typing some keys. This will simplify your interactions with CIDER quite a lot, especially in the beginning. Here's what you'd see if you typed C-c C-d in a Clojure buffer:
Tip
cider-find-var
has built-in support for AVFS. AVFS is an a virtual
file system which allows seamless navigation within archives as if they were
normal directories. When AVFS is mounted, cider-find-var
automatically
opens jar
and zip
files inside AVFS folder instead of attempting to
uncompress the archive.
On Linux-based systems, AVFS is available through the standard package managers. For example, on Debian derivatives:
sudo apt-get install avfs
Once installed, you can put mountavfs
in a place where it will
be invoked
automatically during the startup (.bash_profile
, for
instance). You can also initialize the
avfsd
daemon directly like this:
/usr/bin/avfsd -o allow_root -o intr -o sync_read .avfs
AVFS is not available on Windows but can be installed on MacOS with some effort. Some other uses of AVFS in Emacs include dired-avfs and sunrise-commander.