yamltidy is a formatter for YAML files.

Tired of manually formatting your YAML files? Let yamltidy do it for you!

Examples

ConfigBeforeAfter
Remove Trailing Whitespace
---
v: v0.1
trailing-spaces: fix
steps:  # Workflow steps  
- uses: actions/checkout@v3  
- name: Install deps
  run: |   
    apt-get install cpanminus  
    # The trailing space above ^ is
    # content and doesn't get removed
    cpanm --notest YAML::PP
steps:  # Workflow steps
- uses: actions/checkout@v3
- name: Install deps
  run: |
    apt-get install cpanminus  
    # The trailing space above ^ is
    # content and doesn't get removed
    cpanm --notest YAML::PP
Adjust Indentation
---
v: v0.1
indentation:
  spaces: 2
  block-sequence-in-mapping: 0
steps:  # Workflow steps
    - uses: actions/checkout@v3
    - name: Install deps
      run: |
       apt-get install cpanminus
       cpanm --notest YAML::PP
steps:  # Workflow steps
- uses: actions/checkout@v3
- name: Install deps
  run: |
    apt-get install cpanminus
    cpanm --notest YAML::PP
---
v: v0.1
indentation:
  spaces: 2
  block-sequence-in-mapping: 2
steps:  # Workflow steps
    - uses: actions/checkout@v3
    - name: Install deps
      run: |
       apt-get install cpanminus
       cpanm --notest YAML::PP
steps:  # Workflow steps
  - uses: actions/checkout@v3
  - name: Install deps
    run: |
      apt-get install cpanminus
      cpanm --notest YAML::PP
Add Header and Footer
---
v: v0.1
header: true
footer: true
steps:  # Workflow steps
- uses: actions/checkout@v3
- name: Install deps
  run: |
    apt-get install cpanminus
    cpanm --notest YAML::PP
---
steps:  # Workflow steps
- uses: actions/checkout@v3
- name: Install deps
  run: |
    apt-get install cpanminus
    cpanm --notest YAML::PP
...
Quoting
---
v: v0.1
scalar-style:
  # Remove quotes
  # where possible
  default: plain
"steps":  # Workflow steps
- uses: "actions/checkout@v3"
- name: Install deps
  run: cpanm --notest YAML::PP
  env: { 'glob': '*' }
  "continue-on-error": true
steps:  # Workflow steps
- uses: actions/checkout@v3
- name: Install deps
  run: cpanm --notest YAML::PP
  env: { glob: '*' }
  continue-on-error: true
---
v: v0.1
scalar-style:
  # Add singlequotes
  # to every scalar
  default: single
steps:  # Workflow steps
- uses: actions/checkout@v3
- name: Install deps
  run: cpanm --notest YAML::PP
  env: { glob: '*' }
  # 'true' doesn't get quotes
  # because it is a boolean
  continue-on-error: true
'steps':  # Workflow steps
- 'uses': 'actions/checkout@v3'
- 'name': 'Install deps'
  'run': 'cpanm --notest YAML::PP'
  'env': { 'glob': '*' }
  # 'true' doesn't get quotes
  # because it is a boolean
  'continue-on-error': true
Serialize Aliases
Anchors can be redefined inside a YAML document.
Some libraries don't allow it, and maybe you also want to disallow it.
But manually numbering anchors can be tedious.
yamltidy will add a numeric suffix to all redefined anchors.
---
v: v0.1
aliases:
  serialize: true
- main: &color red
  name: A
  x: *color
  y: *color
- main: &color pink
  name: B
  x: *color
  y: *color
- main: &color_1 red
  name: A
  x: *color_1
  y: *color_1
- main: &color_2 pink
  name: B
  x: *color_2
  y: *color_2
---
v: v0.1
aliases:
  serialize: true
- main: &color_1 red
  name: A
  x: *color_1
  y: *color_1

# New anchor will cause renumbering
- main: &color blue
  name: A2
  x: *color
  y: *color

- main: &color_2 pink
  name: B
  x: *color_2
  y: *color_2
- main: &color_1 red
  name: A
  x: *color_1
  y: *color_1

# New anchor will cause renumbering
- main: &color_2 blue
  name: A2
  x: *color_2
  y: *color_2

- main: &color_3 pink
  name: B
  x: *color_3
  y: *color_3

Features

Currently it can: Plans for the future: It can be configured via a YAML config file, similar to yamllint.

What it can't do:

Examples for a large number of input files can be found here:

Links: