YAML formatter for pre-commit.
This hook formats the indentation of YAML files and
optionally aligns top-level colons.
It uses ruamel.yaml
to do the heavy lifting and preserve comments within YAML files.
Add to .pre-commit-config.yaml
in your git repo:
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.1 # or other specific tag
hooks:
- id: yamlfmt
💡 If a pre-commit hook changes a file, the hook fails with a warning that files were changed.
The default settings are:
mapping
: 4 spacessequence
: 6 spacesoffset
: 4 spacescolons
: do not align top-level colonswidth
: None (use ruamel default)
Given this input:
foo:
bar:
- baz1
- baz2
The default settings result in this output:
---
foo:
bar:
- baz1
- baz2
yamlfmt
only works with valid YAML files, so
I recommend to use yamllint
and yamlfmt
together.
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.21.0 # or higher tag
hooks:
- id: yamllint
args: [--format, parsable, --strict]
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.1 # or other specific tag
hooks:
- id: yamlfmt
Add to .pre-commit-config.yaml
in your git repo:
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.1 # or other specific tag
hooks:
- id: yamlfmt
args: [--mapping, '2', --sequence, '2', --offset, '0', --colons, --width, '150']
If you want to invoke the checks as a git pre-commit hook, run:
# Run on every commit.
pre-commit install
If you want to run the checks on-demand (outside of git hooks), run:
# Run on-demand.
pre-commit run --all-files
The test harness of this git repo uses this approach.
Please see CONTRIBUTING.md.
Please see TESTING.md.
The code in this repo is licensed under the MIT License.