Skip to content

YAML

Files ending in .yaml and .yml are formatted by the YAML pack (rainbow_fmt.languages.yaml, grammar tree-sitter-yaml).

nested:
    deep:
        x:    "quoted"  # trailing
list:
- a
-   b: 1
    c: [1,2]

becomes

nested:
  deep:
    x: "quoted" # trailing
list:
  - a
  - b: 1
    c: [1, 2]

What changes and what does not

The pack changes layout only. Scalars (plain, quoted, multi-line), anchors, aliases, tags, directives and document markers are printed as written.

  • Block mappings and sequences are re-indented to core.indent_size, which is 2 for YAML (the pack's default; [core] indent_size overrides it for every language, [language.yaml] indent_size for YAML only).
  • One space after : and -. A mapping that is a sequence item starts on the dash's line (- b: 1), its other pairs aligned under the first.
  • A sequence that is the value of a key is indented under the key (sequence_indent = "indent"); with "none" it stays at the key's level, as yamllint and many hand-written files have it.
  • Flow collections are spaced [1, 2] and { a: 1 }, and break one item per line when they do not fit; one with a comment inside always breaks.
  • Block scalars (|, >, with chomping indicators) move with their key: their lines are re-indented one level inside it, keeping their relative indentation; |+ keeps its trailing blank lines. A block scalar with an explicit indentation indicator (|2) is kept as written.
  • An anchor or tag on a block collection stays on the key's (or dash's) line: base: &base, then the mapping below it.
  • Comments stay where they are: after the value on its line, or on a line of their own. Blank lines are kept, at most core.max_blank_lines (default 1).
  • Multi-line plain and quoted scalars are kept as written; if the key moves to a deeper indentation than their continuation lines, the file no longer parses and the verifier reports it rather than writing it.

Inline directives (# rainbow: off … # rainbow: on, # rainbow: skip-next) apply to the pairs or items of one mapping or sequence (configuration.md).

Options

Key Values Default
language.yaml.sequence_indent "indent": a sequence under its key is indented; "none": at the key's level "indent"
language.yaml.indent_size the pack's default for core.indent_size 2