Question

Is there a way to use pprint instead of regular print in clojure sublimeREPL

For the moment i had to wrap my code like that to do so:

(clojure.pprint/pprint (for [i (range 10)] {i {:times2 (* i 2) :times3 (* i 3)}}))
=>    ({0 {:times2 0, :times3 0}}
       {1 {:times2 2, :times3 3}}
       {2 {:times2 4, :times3 6}}
       {3 {:times2 6, :times3 9}}
       {4 {:times2 8, :times3 12}}
       {5 {:times2 10, :times3 15}}
       {6 {:times2 12, :times3 18}}
       {7 {:times2 14, :times3 21}}
       {8 {:times2 16, :times3 24}}
       {9 {:times2 18, :times3 27}})

sorry for the dummy example

Was it helpful?

Solution

You can do it with simple nrepl middleware, for example:

(defn pprint-middleware [h]
  (fn [{:keys [code op] :as msg}]
    (if (and (= op "eval") (not (empty? code)))
      (->> #(str "(clojure.pprint/pprint " % ")")
           (update-in msg [:code])
           h)
      (h msg))))

The easiest way to add it to your repl is to config repl-options in youe project.clj:

:repl-options {:nrepl-middleware [nrepl.utils/pprint-middleware]})

Here is the full project.clj example:

(defproject sample "0.1.0"
  :dependencies [[org.clojure/clojure "1.4.0"]]
  :repl-options {:nrepl-middleware [sample.utils/pprint-middleware]})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top