Readme: treefmt — one CLI to format your repo

Treefmt applies all the needed formatters to your project with one command line.

Motivation

Before making contributions to any project, it’s common to get your code formatted according to the project’s standards. This task seems trivial from the first sight — you can simply set up the required language formatter in your IDE. But contributing to multiple projects requires more effort: you need to change the code formatter configs each time you switch between projects, or call formatters manually.
Formatting requires less effort if a universal formatter for multiple languages is in place, which is also project-specific.

About treefmt

Treefmt runs all your formatters with one command. It’s is easy to configure and fast to execute.
[![asciicast](https://asciinema.org/a/cwtaWUTdBa8qCKJVp40bTwxf0.svg)](https://asciinema.org/a/cwtaWUTdBa8qCKJVp40bTwxf0)
Its main features are:
Providing a unified CLI and output: You don’t need to remember which formatters are required for each project. Once you specify the formatters in the config file, you can trigger all of them with one command and get a standardized output.
Running all the formatters in parallel: A standard script loops over your folders and runs each formatter consequentially. In contrast, treefmt runs formatters in parallel. This way, the formatting job takes less time.
Caching the changed files: When formatters are run in a script, they process all the files they encounter, no matter whether the code has changed. This unnecessary work can be eliminated if only the changed files are formatted. Treefmt caches the changed files and marks them for re-formatting.
Just type `treefmt` in any folder to reformat the whole code tree. All in all, you get a fast and simple formatting solution. Find the project wiki
.

Installation

You can install the tool by downloading the binary. Find the binaries for different architectures
. Otherwise, you can install the package from the source code — either with , or with help of
. We describe the installation process in detail in the
.

Usage

In order to use treefmt in your project, make sure the config file treefmt.toml is present in the root folder and is edited to your needs. You can generate it with:
$ treefmt --init
You can run treefmt in your project root folder like this:
$ treefmt
To explore the tool’s flags and options, type:
$ treefmt --help
You can also integrate `treefmt` into your CI.

Configuration

Fomatters are specified in the config file treefmt.toml, which is usually located in the project root folder. The generic way to specify a formatter is like this:
[formatter.<name>]
command = "<formatter-command>"
options = [“<formatter-option-1>”...]
includes = ["<glob>"]
For instance, if you want to use rustfmt on your Rust project on a Nix project, then the treefmt.toml file will look like this:
[formatter.my_nix_formatter]
command = "nixpkgs-fmt"
includes = ["*.nix"]

[formatter.rust]
command = "rustfmt"
options = ["--edition", "2018"]
includes = ["*.rs"]
Before specifying the formatter in the config, make sure it’s installed. See the for more examples.

Compatibility

Treefmt works with any formatter that adheres to the . For instance, you can go for:
for Java,
gofmt for Golang
Prettier for JavaScript/HTML/CSS
Find the full list of supported formatters here.

Upcoming features

IDE integration
Pre-commit hooks
Effective support of multiple formatters

Related projects

: unifies file indentations configuration on a per-project basis.
: an opinionated code formatter for a number of languages.
: a project by GitHub to lint all of the code.
: a framework for managing and maintaining multi-language pre-commit hooks.

Contributing

All contributions are welcome! We try to keep the project simple and focused. Please refer to the for more information.

License

Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion will be licensed under the without any additional terms or conditions.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.