zprint is a library and command line tool providing a variety of pretty printing capabilities for both Clojure code and Clojure/EDN structures. It can meet almost anyone's needs. As such, it supports a number of major source code formatting approaches.
zprint does far more than just properly indent code. Before:
(defn change-start-column [new-start-column style-vec [inline-comment-index
start-column spaces-before :as comment-vec]] (if (zero? inline-comment-index)
style-vec (let [delta-spaces (- new-start-column start-column) new-spaces
(+ spaces-before delta-spaces) previous-element-index (dec
inline-comment-index) [s c e :as previous-element] (nth style-vec
previous-element-index) new-previous-element (cond (= e :indent) [(str "\n"
(blanks new-spaces)) c e] (= e :whitespace) [(str (blanks new-spaces))
c e 26] :else nil)] (assoc style-vec previous-element-index
new-previous-element))))
After:
(defn change-start-column
[new-start-column style-vec
[inline-comment-index start-column spaces-before :as comment-vec]]
(if (zero? inline-comment-index)
style-vec
(let [delta-spaces (- new-start-column start-column)
new-spaces (+ spaces-before delta-spaces)
previous-element-index (dec inline-comment-index)
[s c e :as previous-element] (nth style-vec previous-element-index)
new-previous-element
(cond (= e :indent) [(str "\n" (blanks new-spaces)) c e]
(= e :whitespace) [(str (blanks new-spaces)) c e 26]
:else nil)]
(assoc style-vec previous-element-index new-previous-element))))
)]}
, which also fall on their own line, either because of
:respect-nl
or because of a comment. Some have requested that
these characters be indented to the indent of the structure
involved. Others have requested that (unless required by a
comment) they be brought up to the previous line so that they are
not "trailing right" characters anymore. Both capabilties are
now supported, the first by :indent-trailing-right?
and the
second by :collapse-trailing-right?
, for lists, vectors, maps
and sets. See the reference manual for details.:no-wrap-after
has been around for a while. This lets you
specify an element where you would prefer the next element be on
the same line. It has been completely re-implemented and now
works well in many more situations.{:files {:glob "<filespec>"}}
key-value pair for the options map.:sort-require
will sort the requires in an ns
macro,
as suggested by
how-to-ns.
This will interoperate with :ns-justify
well, but be sure and put
the :sort-require
to the left of (i.e., before) the :ns-justify
.
This will sort the elements of the :refer
vector as well.:ns-justify
has been modified to support explicit parameters
for the variance in the :require
, :require-macros
and :import
sections of the ns
macro. They are :require-max-variance
,
:require-macros-max-variance
and :import-max-variance
and may
be used in a style map. For example {:style-call :ns-justify :require-max-variance 1000}
will try as hard as possible to justify
the :require
list in an ns
macro.zprintma-1.3.0
from the release to get the Apple Silicon version.bbin
.
It starts very quickly and runs faster than the uberjar on even very
large files. If using a task, you don't need to install a new version,
just edit bb.edn
. See the simple detals hereIn addition, zprint is very handy to use at the REPL.
Maybe one of the existing "styles" will meet your needs. All you have to
do is put {:style ...}
on the command line or as the third argument
to a zprint call. For example, {:style :community}
or
{:style :respect-bl}
.
Some commonly used styles:
quote
, deref
, var
, unquote
in structuresLeiningen (via Clojars)
zprint has been tested in each of the following environments:
planck
2.28.0 (Clojurescript 1.11.132)It requires tools.reader
at least 1.0.5, which all of the environments
above contain.
The last zprint release built with Clojure 1.8 was [zprint "0.4.15"].
In addition to the zprint dependency, you also need to include the following library when using Clojure 1.8:
[clojure-future-spec "1.9.0-alpha17"]
:style
quote
, deref
, var
, unquote
in structureslet
binding vectorscond
, assoc
pairsInformation on testing and development can be found here.
Note: Changed the default branch to main
.
A number of folks have contributed to zprint, not all of whom show up on GitHub because I have integrated the code or suggestions manually. Thanks for all of the great contributions!
~ @stuff
formatted as ~@stuff
: @frenchy64sci.core
in babashka: @borkduderewrite-cljs
dependency to 0.4.5
@rundis/--url
and --url-only
: @coltnzUTF-8
locale to build the native image: @mynomoto:respect-bl
: @agriffis:option-fn
and :fn-format
for enhanced vector formatting: @milankinenspec.cljc
: @Quezionns
macro: @pesterhazyThanks to everyone who has contributed fixes as well as everyone who has reported an issue. I really appreciate all of the help making zprint better for everybody!
At the core of zprint
is the rewrite-clj
library originally
created by Yannick Scherer, ported to Clojurescript by Magnus
Rundberget, and recently merged into a single, supported, documented,
and updated library by Lee Read. This is a great library! I would not have
attempted zprint
if rewrite-clj
didn't exist to build upon.
Additionally, allowing options maps containing functions to be read
from files safely is made possible by sci
, the Small Clojure Interpreter
by Michael Borkent (@borkdude). This is a very well designed and
implemented addition to Clojure that required almost no effort to integrate
into zprint.
Copyright © 2016-2025 Kim Kinnear
Distributed under the MIT License. See the file LICENSE for details.
Can you improve this documentation? These fine people already did:
Kim Kinnear, Brett Rowberry, lread, Aron Griffis, Quest Yarbrough, vemv, Andrea Richiardi & Brian HurlowEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close