yamllint YAML linter

Version 1.38.0 · source

Start here

yamllint <file>
Lint one file
yamllint .
Lint a whole tree
yamllint -d relaxed <file>
Use the lenient preset
yamllint -c <config> <file>
Use your config
yamllint -f parsable .
One line per problem
yamllint -s .
Warnings fail too
yamllint --list-files .
See what gets linted
# yamllint disable-line
Silence one line

Concepts

  1. -c / -d flag wins if given
  2. .yamllint[.yaml/.yml] cwd or a parent
  3. $YAMLLINT_CONFIG_FILE env var
  4. ~/.config/yamllint/config then built-in default
How a config is chosen

Running

yamllint <file> <dir>
Lint files and directories
yamllint .
Lint every YAML file below here
Directories only pick up *.yaml, *.yml and .yamllint unless yaml-files says otherwise.
cat <file> | yamllint -
Lint standard input
yamllint -s .
Exit non-zero on warnings too
yamllint --no-warnings .
Show errors only
yamllint --list-files .
List files that would be linted
yamllint -f parsable .
file:line:col output for scripts
yamllint -f github .
GitHub Actions annotations
yamllint -f colored .
Colored output
yamllint -f auto .
Pick format from environment

Config selection

yamllint -c <config> <file>
Use a config file
yamllint -d relaxed <file>
Built-in lenient preset
yamllint -d default <file>
Built-in default preset
yamllint -d "{extends: default, rules: {line-length: {max: 120}}}" .
Inline config on the CLI
YAMLLINT_CONFIG_FILE=<path> yamllint .
Config via environment
.yamllint
Per-project config file name
Also .yamllint.yaml and .yamllint.yml; searched in the current dir and its parents.
~/.config/yamllint/config
User-wide fallback config

Config file keys

extends: default
Start from the default preset
extends: relaxed
Start from the lenient preset
rules:
Map of rule name to settings
<rule>: enable
Turn a rule on with defaults
<rule>: disable
Turn a rule off
level: warning
Rule problems become warnings
level: error
Rule problems become errors
ignore: |
Gitignore-style paths to skip
<rule>: {ignore: <patterns>}
Skip paths for one rule only
ignore-from-file: .gitignore
Read ignore patterns from a file
Cannot be combined with ignore.
yaml-files: ["*.yaml", ".yamllint"]
Which names count as YAML

Inline disables

# yamllint disable-line
Skip this line
Put it at the end of the line or on the line above.
# yamllint disable-line rule:<rule>
Skip one rule on this line
# yamllint disable
Skip all rules from here
# yamllint enable
Resume checking
# yamllint disable rule:<rule>
Skip one rule from here
# yamllint enable rule:<rule>
Resume one rule
# yamllint disable-file
Skip the whole file
Must be the first line of the file.
# yamllint disable rule:<a> rule:<b>
Skip several rules at once

Whitespace rules

indentation: {spaces: 2}
Fixed indent width
indentation: {spaces: consistent}
Any width, used consistently
indentation: {indent-sequences: consistent}
Allow both list indent styles
true forces indented lists under keys, false forbids it, consistent allows either per file.
line-length: {max: 120}
Longer lines allowed
line-length: {allow-non-breakable-words: true}
Long URLs are exempt
line-length: {allow-non-breakable-inline-mappings: true}
Exempt long key: value lines
line-length: disable
No line length limit
trailing-spaces: enable
Flag spaces at line end
empty-lines: {max: 1}
Limit consecutive blank lines
new-line-at-end-of-file: enable
Require a final newline
new-lines: {type: unix}
LF line endings (also dos, platform)
hyphens: {max-spaces-after: 1}
Spaces after a list dash

Syntax and style rules

document-start: {present: false}
Do not require ---
document-start: disable
Ignore the --- check
document-end: {present: true}
Require ... at the end
truthy: {allowed-values: ["true", "false"]}
Which bool words are allowed
truthy: {allowed-values: ["true", "false", "on"]}
Allow on (GitHub Actions)
truthy: {check-keys: false}
Do not flag keys like on:
comments: {min-spaces-from-content: 1}
Gap before inline comment
comments: {require-starting-space: true}
Need # then a space
comments-indentation: disable
Allow odd comment indent
colons: {max-spaces-after: -1}
No limit after colon (aligned values)
braces: {min-spaces-inside: 0, max-spaces-inside: 1}
Spaces inside { }
brackets: {max-spaces-inside: 1}
Spaces inside [ ]
commas: {max-spaces-after: 1}
Spaces after commas
braces: {forbid: true}
Ban flow mappings {a: 1}
brackets: {forbid: true}
Ban flow lists [a, b]

Value and key rules

key-duplicates: enable
Same key twice in a mapping
On by default; the most useful catch for copy-pasted config.
key-duplicates: {forbid-duplicated-merge-keys: true}
Also duplicates via <<
key-ordering: enable
Require sorted keys
key-ordering: {ignored-keys: ["<regex>"]}
Exempt keys from sorting
anchors: {forbid-unused-anchors: true}
Flag &anchors nobody uses
anchors: {forbid-duplicated-anchors: true}
Flag a repeated &anchor name
anchors: {forbid-undeclared-aliases: true}
Flag *alias with no anchor
quoted-strings: {quote-type: double}
Force one quote style
quoted-strings: {required: only-when-needed}
Quote only when necessary
quoted-strings: {allow-quoted-quotes: true}
Keep quotes holding other quotes
empty-values: enable
Flag key: with no value
octal-values: enable
Flag 0777 style numbers

Recipes

Typical project .yamllint
extends: defaultrules:  line-length:    max: 120    level: warning  document-start: disable  truthy:    check-keys: false
Skip generated and vendor files
extends: defaultignore: |  node_modules/  charts/*/templates/  .github/workflows/generated-*.yml
CI: fail on any problem, annotate PRs
yamllint -s -f github .
Disable a rule for one block
# yamllint disable rule:line-lengthurl: https://example.org/a/very/long/path?with=query# yamllint enable rule:line-length