Skip to content

colons

What this rule does

Controls the number of spaces around mapping colons (:).

Why this matters

  • Readability. Consistent spacing makes columnar layouts easier to scan, especially in configuration files with many short keys.
  • Avoids ambiguity. Stray spaces around the colon can hide subtle parsing bugs, particularly when keys contain values that look like flow-style content.

Configuration

[rules.colons]
level = "error"
max-spaces-before = 0
max-spaces-after = 1
Option Default Description
max-spaces-before 0 Maximum spaces between the key and the :. Use -1 to disable.
max-spaces-after 1 Maximum spaces between the : and the value. Use -1 to disable.

Examples

✅ Allowed (defaults)

key: value
object:
  - a
  - b

❌ Reported (defaults)

key : value
key:   value

✅ Allowed (with max-spaces-after: 2)

first:  1
second: 2
third:  3

Alias mapping keys

A YAML anchor/alias name may legally contain :, so *anchor: welds into an alias to an anchor named anchor: (a parse error here, since no mapping colon remains). Using an alias as a mapping key therefore requires one separating space before the colon — *anchor : value. When exactly that one space is present the colon's spacing is not reported (the rule defers to the parser's view of the alias); more than one space before the colon is reported as usual.

base: &a name
*a : value     # allowed: the one required separating space is not reported
base: &a name
*a  : value    # reported (2:4): too many spaces before colon

Automatic fixing

This rule does not auto-fix; correct spacing manually.

  • commas — the analogous rule for flow collection commas.
  • hyphens — spacing for block sequence hyphens.