Page
Library
Module
Module type
Parameter
Class
Class type
Source
OCamlFormat is a tool to format OCaml code.
OCamlFormat works by parsing source code using the OCaml compiler's standard parser, deciding where to place comments in the parse tree, and writing the parse tree and comments in a consistent style.
See the source code of OCamlFormat itself and Infer for examples of the styles of code it produces.
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:
Normalize
.equal_impl
or equal_intf
).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. 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:
The compact
profile is similar to ocamlformat
but opts for a generally more compact code style.
The sparse
profile is similar to ocamlformat
but opts for a generally more sparse code style.
If no profile is selected, the conventional
one is used.
The full options' documentation is available in [ocamlformat-help.txt] and through ocamlformat --help
. Options can be modified by the means of:
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 not set, .ocamlformat files outside of the project (including the one in XDG_CONFIG_HOME
) are not 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. If no config 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.
OCamlFormat can be installed with opam
:
opam install ocamlformat
Alternately, see ocamlformat.opam
for manual build instructions.
As mentioned in the Options section, when the option --disable-outside-detected-project
is set, .ocamlformat files outside of the project (including the one in XDG_CONFIG_HOME
) are not 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. If no config file is found, then the formatting is disabled.
This feature is often the behavior you can expect from OCamlFormat when it is directly run from your text editor, so it is advised to use this option.
$(opam config var share)/emacs/site-lisp
to load-path
(as done by opam user-setup install
)(require 'ocamlformat)
to .emacs
.emacs
to bind C-M-<tab>
to the ocamlformat command and install a hook to run ocamlformat when saving:(add-hook 'tuareg-mode-hook (lambda ()
(define-key tuareg-mode-map (kbd "C-M-<tab>") #'ocamlformat)
(add-hook 'before-save-hook #'ocamlformat-before-save)))
To pass the option --disable-outside-detected-project
(or --disable
) to OCamlFormat:
emacs
M-x customize-group⏎
then enter ocamlformat⏎
Enable
(by default), Disable
or Disable outside detected project
C-x C-s
) then enter yes⏎
and exitOther OCamlFormat options can be set in .ocamlformat configuration files.
ocamlformat
binary can be found in PATHOptional: You can change the options passed to OCamlFormat (to use the option --disable-outside-detected-project
for example), you can customize NeoFormat with:
let g:neoformat_ocaml_ocamlformat = {
\ 'exe': 'ocamlformat',
\ 'no_append': 1,
\ 'stdin': 1,
\ 'args': ['--disable-outside-detected-project', '--name', '"%:p"', '-']
\ }
let g:neoformat_enabled_ocaml = ['ocamlformat']
OCamlFormat is documented in its man page and through its internal help:
ocamlformat --help
man ocamlformat
You can also view it online.
OCamlFormat is influenced by and follows the same basic design as refmt
for Reason, but outputs OCaml instead of Reason.
This tool is not able to deal directly with Reason code (*.re
/*.rei
files), but it is possible to first convert these files to ocaml syntax using refmt -p ml
and then running ocamlformat
on this output.
See CONTRIBUTING for how to help out.
OCamlFormat is MIT-licensed.