Skip to main content

yaml-sort

Deterministic YAML key ordering. Sorts mapping keys alphabetically (bottom-up) while preserving comments and byte-for-byte formatting, so running it produces no diff when a file is already sorted.

Designed to keep chezmoi-managed config diffs clean.

Why not just yaml.load → sort → yaml.dump?

A library round-trip rewrites the whole file: it drops comments, re-quotes strings, re-indents everything, and normalizes flow/block style. That adds diff noise on every run. yaml-sort instead parses the block structure and re-emits it verbatim, moving only what it needs to (mapping keys).

Features

  • Sorts mapping keys alphabetically, bottom-up — including inside list items and nested maps.
  • Never reorders list items (sequence order is meaningful).
  • A comment that is the last thing in a file (a footer) stays pinned at the very bottom — it is not dragged into the middle when the neighbouring key re-sorts.
  • For a list item that opens a map (- api_key: none then base_url:…), the anchor key fused to the - is sorted with its siblings and the - re-fuses to whichever key sorts first, keeping every list entry fully alphabetical.
  • --list-indent keep (default): preserve existing indentation byte-for-byte.
  • --list-indent N: re-indent sequence items N spaces right of their parent key (0 = aligned, 2 = conventional indented style).
  • --check, --diff, --stdin modes.

Install

pip install yaml-sort        # from PyPI (once published)
# or, from a checkout:
pip install -e .

Usage

yaml-sort <file>                  Sort file in-place
yaml-sort <file> --check          Exit 0 if sorted, 1 if not
yaml-sort <file> --diff           Show what would change
yaml-sort <file> --list-indent 2  Also re-indent lists 2 spaces under their key
yaml-sort --stdin                 Read stdin, write sorted to stdout

Library use

from yaml_sort import sort_text

sorted_yaml = sort_text(raw_text)            # byte-preserving
sorted_yaml = sort_text(raw_text, 2)         # re-indent lists to 2 spaces

Development

pip install -e .[dev]
pytest

Metadata

Release files for yaml-sort 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yaml-sort 0.2.1
File Size Uploaded
yaml_sort-0.2.1.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for yaml-sort 0.2.1
File Interpreter ABI Platform
yaml_sort-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 28.4 kB

Release files / yaml_sort-0.2.1.tar.gz

Download URL yaml_sort-0.2.1.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
acb0b90765c9b45d597aef958efaf3c4fd3471715d6c33d2ff9eb3e6aacd8414
BLAKE2b-256 checksum
How to use checksums
bb00ee93dfe766793714811387ffa329b1b26df554d96496eaf0dabc3a73a663
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.

Transparency log

Release files / yaml_sort-0.2.1-py3-none-any.whl

Download URL yaml_sort-0.2.1-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b5699f66f8cb92d0859c59eef81a1522ddb057b39973d83c20bc7d32312e01c
BLAKE2b-256 checksum
How to use checksums
3273dcdacf5bd191c567641681b7895cf90676e4e34c2461c9179da3846c01a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page