53

I have played with a lot of code in a repl console, how can I clear it? I would like a fresh one without restarting it. Can that be done?

Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
Belun
  • 4,151
  • 7
  • 34
  • 51

8 Answers8

54

If you want to clear the current namespace of all temporary variables and functions you declared you can use this one liner (or make a function of it) :

(map #(ns-unmap *ns* %) (keys (ns-interns *ns*)))

or

(ns myutil)
(defn ns-clean
       "Remove all internal mappings from a given name space or the current one if no parameter given."
   ([] (ns-clean *ns*)) 
   ([ns] (map #(ns-unmap ns %) (keys (ns-interns ns)))))
(ns mytest)

... make loads of junk ...

(myutil/ns-clean)

... great!!! I can now make all new junk ... 

It does not claim to give you a squeaky clean namespace, just one with less of the junk which usually accumulates in a typical repl session.

Use with caution : do not pull the rug from under your feet!

seh
  • 14,999
  • 2
  • 48
  • 58
Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
25

If you are running the repl through a terminal window (eg: Terminal.app on MacOS or xterm/aterm/urxvt etc on linux) then you can type Control-L and it should clear the terminal window and give you a new repl prompt. However all macros/atoms you previously defined are still going to be in memory, so this is just a "Cosmetic" clear.

Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
  • the initial question was about cosmetically clearing the repl. later i realized that clearing could also mean something else :) – Belun Sep 04 '10 at 11:01
19

In EMACS/slime REPLs C-c C-o clears the last output (in case you've typed something that gave a very long answer) C-c M-o clears the whole thing

In GNOME terminals, you've got a menu option Terminal/Reset and Clear

John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
  • 1
    Note that C-c M-o does not erase any work actually interpreted by the REPL already, it just clears the screen. Any definitions are still there. – johnbakers Oct 29 '13 at 05:18
7

The shorcut to clean the whole buffer : C-u C-c C-o

The shortcut to clean the last output : C-c C-o

Note, the old way was : C-c M-o

Also, cider-repl-clear-buffer (which is bound to C-ENTER s-c on my machine)

nha
  • 17,623
  • 13
  • 87
  • 133
4

If you are using Emacs + nREPL, you can either:

  • Run Mx nrepl-clear-buffer or
  • Run Cc Mo

If the key binding is not enabled, run Mxnrepl-interaction-mode to enable it. You can find other useful shortcuts in nrepl.el and/or customize the key bindings to fit your needs.

Note: you can find all nREPL's key bindings in your system by running M-x v nrepl-mode-map and following the nrepl.el link.

a2ndrade
  • 2,403
  • 21
  • 19
2

I use the Emacs command cider-repl-clear-buffer via M-x. One might also use cider-repl-clear-output with a prefix argument: C-u C-c C-o.

Dave Liepmann
  • 1,555
  • 1
  • 18
  • 22
0

It depends what you mean by 'clean'. To remove all namespaces within a 'package' you can use:

(mapv remove-ns 
  (map symbol 
    (filter #(.startsWith % "org.mycompany") 
      (map str (all-ns)))))
Rachel K. Westmacott
  • 2,038
  • 20
  • 15
0

For users of the Cursive IDE plugin for IntelliJ IDEA who end up here, like me:

You can "cosmetically" clear the REPL (keeping your def'd symbols etc.) by clicking on this icon at the top of the REPL frame:

Clear REPL icon

There's no default key binding for this, but you can easily add one by opening your preferences, navigating to Keymap > Plugins > Cursive, and adding a binding for "Clear output for current REPL".

Alternatively, you can right-click in the editor and access "Clear output for current REPL" via the REPL commands.

fordy
  • 2,520
  • 1
  • 14
  • 22