Evaluating individual Expressions

Evaluating individual expressions is a very effective way to test the actions of your code as you write it.

Evil Normal Command
, e e cider-eval-last-sexp
, e f cider-eval-defun-at-point
, e ; cider-eval-defun-at-point-as-comment
, e w cider-eval-last-sexp-and-replace

Evaluate expressions in the source code buffer

Evaluate the namespace and function definition in src/spacemacs-workshop/core.clj.

Then write an expression to call foo with a string argument, eg. (foo "Spacemacs ")

Now change the definition of the function foo by replacing the println function with the str function. Both functions take the same arguments.

Replace code with result

A powerful way to understand your code or explain to others what an expression does is to replace the code with result of its evaluation. This technique is especially useful for expressions that are nested within another expressions.

, e w - replace previous expression with its return value


You can see the Clojure code that is created when evaluating a macro using the function cider-macroexpand-1 or cider-macroexpand-all. Expanding a macro shows you the code generated by the macro reader.

  • , e m - replace previous expression with its return value

In this example we expand a simple defn macro

(defn foo
  "I don't do a whole lot."
  (println x "Hello, World!"))

Spacemacs - Clojure - Evaluate - Macro-expand a defn macro


To evaluate the whole namespace of a Clojure file, select the buffer containing the clojure file and evaluate the buffer. This will read all the expressions from top to bottom and show the result in the mini-buffer

You can also select a region of the code, C-SPC, and evaluate that region. This is useful for evaluating several expressions at once, which will again be evaluated top to bottom, without having to evaluate the whole buffer. Selecting a whole buffer and evaluating the region is the same as evaluating the buffer.

Spacemacs Major mode Emacs Evil Command
SPC m e b M-RET e b C-c C-k , m e b cider-eval-buffer
SPC m e r M-RET e r C-c C-r , m e r cider-eval-region

Note With the src/spacemacs-worshop/core.clj file open, evaluate the whole namespace

The results of print or println expressions are sent to the REPL buffer.

TODO: Insert image / video

results matching ""

    No results matching ""