Library
Module
Module type
Parameter
Class
Class type
OCamlFormat can be installed with opam
:
opam install ocamlformat
Alternatively, it can be built manually with:
# To properly set `ocamlformat --version`
dune subst
dune build @install
First of all, make sure you have an .ocamlformat
file at the root of your project. Setting up your project to use the default profile and the OCamlFormat version you installed (hopefully the last one) in this .ocamlformat
file is considered good practice:
profile = default
version = 0.26.2
To manually invoke OCamlformat the general command is:
ocamlformat [OPTION]... [SRC]...
The most common usecase involves using the Dune build system, once your project is correctly setup (see Dune's manual) you can reformat your project using:
dune build @fmt
With this command, Dune will apply the appropriate autoformatter on each kind of file (where OCamlFormat is the one called on .ml, .mli, .mlt, .eliom and .eliomi files). All files are formatted, unless explicitly excluded (see below).
There are a number of preset code style profiles, selected using the --profile
option by passing --profile=<name>
on the command line or adding profile = <name>
to an .ocamlformat
configuration file. The available profiles are:
conventional
(also known as default
);ocamlformat
;janestreet
.It is considered a good practice to set a profile in the .ocamlformat
configuration file of a project, even if you plan on using the default
profile, explicitly setting profile = default
is recommended.
Each profile is a collection of settings for all options, overriding lower priority configuration of individual options. So a profile can be selected and then individual options can be overridden if desired.
The conventional
(or default
) profile aims to be as familiar and "conventional" appearing as the available options allow.
The ocamlformat
profile aims to take advantage of the strengths of a parsetree-based auto-formatter, and to limit the consequences of the weaknesses imposed by the current implementation. This is a style which optimizes for what the formatter can do best, rather than to match the style of any existing code. Instead of familiarity, the focus is on legibility, keeping the common cases reasonably compact while attempting to avoid confusing formatting in corner cases. General guidelines that have directed the design include:
If no profile is selected, the default
one is used.
The full options' documentation is available on this page or through ocamlformat --help
. Options can be modified by the means of:
.ocamlformat
configuration file with an option = VAL
lineOCAMLFORMAT
environment variable: OCAMLFORMAT=option=VAL,...,option=VAL
[@@@ocamlformat "option=VAL"]
attribute in the processed file[@@ocamlformat "option=VAL"]
attribute on an expression in the processed file.ocamlformat
files in the containing and all ancestor directories for each input file are used, as well as the global .ocamlformat
file defined in $XDG_CONFIG_HOME/ocamlformat
. The global .ocamlformat
file has the lowest priority, then the closer the directory is to the processed file, the higher the priority.
When the option --enable-outside-detected-project
is set, .ocamlformat
files outside of the project (including the one in XDG_CONFIG_HOME
) are read. The project root of an input file is taken to be the nearest ancestor directory that contains a .git
or .hg
or dune-project
file. When this option is not set, .ocamlformat
files outside of the project are ignored. If no configuration file is found, formatting is disabled.
An .ocamlformat-ignore
file specifies files that OCamlFormat should ignore. Each line in an .ocamlformat-ignore
file specifies a filename relative to the directory containing the .ocamlformat-ignore
file. Lines starting with #
are ignored and can be used as comments.
On existing projects, it is likely an .ocamlformat
file is already present, specifying a field version = A.B.C
(e.g. 0.20.0
).
It is the user's responsability to install the appropriate OCamlFormat binary, requesting an older version if one's project requires it:
opam install ocamlformat.0.20.0
Setting an OCamlFormat version in the configuration file is a good practice to ensure every contributor of a project gets the same formatting.
OCamlFormat requires source code that meets the following conditions:
.ml
), an interface (.mli
) or a sequence of toplevel phrases (.mlt
). dune files in OCaml syntax also work.Under those conditions, OCamlFormat is expected to produce output equivalent to the input. As a safety check in case of bugs, prior to terminating or modifying any input file, OCamlFormat enforces the following checks:
.equal
).