OCamlformat - OCaml Code Formatter

Format OCaml code with OCamlformat. Easily beautify, validate, and standardize your OCaml source files for better readability and consistency.

OCamlformat

OCaml Code Formatting with OCamlformat

OCamlformat is a powerful and opinionated code formatter for the OCaml programming language. It helps maintain consistent code style across projects, improving readability and reducing cognitive load for developers. By automating the formatting process, OCamlformat ensures that all OCaml code adheres to a predefined set of style guidelines, making it easier to review and contribute to codebases.

Key OCamlformat Commands and Usage

Below are common commands for using OCamlformat to manage your OCaml code style:

# ocamlformat
# Code formatting tool for OCaml.

# Format a single OCaml file in place
ocamlformat --inplace filename.ml

# Read from stdin and output to stdout
ocamlformat - 

# Format all OCaml files in the current project
ocamlformat --root . 

# Check if files need formatting (useful for CI pipelines)
ocamlformat --check filename.ml

# Format according to a specific profile (e.g., conventional)
ocamlformat --profile=conventional filename.ml

# Format files with a specified margin (e.g., 80 characters)
ocamlformat --margin=80 filename.ml

# Specify a custom ocamlformat configuration file
ocamlformat --name=path/to/.ocamlformat filename.ml

# List all available options
ocamlformat --help

OCamlformat Configuration and Profiles

OCamlformat supports various configuration options and profiles to tailor the formatting to your project's needs. You can specify a custom configuration file using the --name flag or leverage predefined profiles like conventional. Adjusting the margin with --margin is also a common practice to ensure code fits within specific line limits.

Integrating OCamlformat into Your Workflow

For continuous integration, the --check option is invaluable. It allows you to verify that all files in your project are correctly formatted without modifying them. This ensures that only well-formatted code is merged, maintaining code quality standards.

Further Resources for OCaml Formatting