How to change output formatting in REPL when started by `clj` command?

Hi Everybody,

Could you please tell me how to change formatting of output when values are returned in REPL when I start it by clj command?

I’d like to get maps without commas:

{:change 1 :this 2}

not: {:change 1, :this 2}

Alex

There is no easy way to do this. There is no config option that controls this for the default printer implementation. Only way to get this would be starting a custom REPL loop with the Print overriden with an implementation that doesn’t add “,”. Something like fipp may support this, not sure.

I’m curious as to why you want maps output without the commas? Comma is whitespace, after all.

You “could” have an alias that launches a repl using clojure.main/repl with a different :print function supplied. This function could intercept the default pr invoked on the object and scrape out commas from the printed representation. If it’s a string, let is pass through, otherwise strip commas from the print and write to out.

The only other option is to intercept all the print-method implementations and wrap them somehow. Maybe clone the print-method multimethod and wrap it somehow.

If it is that you want the output for something else (outside the REPL use) where the commas are not wanted, you could give the map to cljfmt with the proper options (I don’t recall which right now) but one of them cleans up whitespace.

Just personal preference, looks better without.

Thank you everybody. I was thinking that there might be an easier option but it’s not. And that’s cool. I’ll just live with it ;-).

1 Like

For some reason I thought I’d remember seeing a dynamic var for that, but must have been for something else.

Maybe this: *print-namespace-maps* ? Is there a doc which lists all such vars? :thinking:

That var determines whether to print {:foo/bar 1} or #:foo{:bar 1}:

dev=> (println {:foo/bar 1})
#:foo{:bar 1}
nil
dev=> (binding [*print-namespace-maps* false]
#_=> (println {:foo/bar 1}))
{:foo/bar 1}

The various public dynamic vars in clojure.core are listed here: clojure.core - Clojure v1.10.2 API documentation

1 Like