A command-line tool to sort YAML files by keys while preserving comments, structure, and formatting.
- Preserves comments - Maintains all inline and standalone comments
- Customizable sorting - Choose between alphabetical or custom key order
- Format preservation - Keeps original indentation and style
- Multi-document support - Handles YAML files with multiple documents (
---separators) - Strict mode - Optionally validates YAML during processing
go install github.com/ffais/yaml-sort@latestDownload from the Releases page
Basic sorting:
yaml-sort -f input.yaml -o sorted.yamlSort with custom key order:
yaml-sort -f input.yaml -o sorted.yaml -k "name,version,dependencies"Sort in-place (modify file directly):
yaml-sort -f input.yaml -i -f, --file string Input YAML file
-o, --output string Output file (defaults to stdout if not specified)
-i, --in-place Modify input file directly
-k, --keys string Comma-separated list of keys for custom ordering
-s, --strict Enable strict YAML parsing
-v, --version Display version information
-h, --help Show help message
- Sort a Kubernetes manifest:
yaml-sort -f deployment.yaml -o sorted-deployment.yaml- Sort with specific key priority:
yaml-sort -f config.yaml -k "apiVersion,kind,metadata,spec"- Pipe input/output:
cat input.yaml | yaml-sort > sorted.yamlPull requests are welcome! Please ensure:
- Tests pass (
go test ./...) - Code is formatted (
go fmt) - New features include appropriate tests
MIT - See LICENSE file for details.