A YAML file is plain text with a .yml or .yaml extension, using indentation and a few symbols (colons, dashes, quotes) to structure data. You can write one in any text editor in about ten minutes once you know the six core rules.
Time 10 min · Steps 7 · Level Easy
What a YAML File Looks Like Before You Start
YAML (short for "YAML Ain't Markup Language") stores settings and data as key-value pairs, and it is the standard format for Docker Compose files, GitHub Actions workflows, Kubernetes configs and many app settings files. Success looks like this: a file that a validator parses with no errors, and a program that reads the values you intended. You need nothing but a plain text editor — Notepad, TextEdit (in plain text mode), VS Code or any equivalent — and a place to save the file.
How to Write a Basic YAML File
- Open a plain text editor and create a new file. In VS Code choose File > New File; in Notepad just open the app. Avoid word processors like Word or Google Docs, because they add hidden formatting that breaks YAML.
- Write your first key-value pair on one line, with a colon and a space between them:
name: Ajapa. The space after the colon is required —name:Ajapais a syntax error. - Add nested keys with two-space indents. Child keys sit on their own lines, indented two spaces further than their parent:
server:on one line, thenport: 8080on the next. Never use tabs — YAML forbids them; use spaces only. - Create lists with a dash and a space, one item per line at the same indent level:
- apples,- rice,- bread. A list can also be a value:fruits:followed by the dashed items indented two spaces. - Quote strings that could be misread. Wrap values in double or single quotes when they start with a special character, contain a colon, or look like a number you want kept as text:
answer: "yes",version: "1.0". Plain numbers, true/false and null are written bare:retries: 3,enabled: true. - Add comments with a hash. Anything after
#on a line is ignored:port: 8080 # override in production. Comments are how you leave notes for whoever reads the file next. - Save the file with a .yml or .yaml extension. In your editor's save dialog, name it something like
config.ymland set the file type to "All files" if the editor tries to append.txt. Both extensions mean the same thing;.ymlis the more common choice in Docker Compose and GitHub Actions.
A Complete YAML File You Can Copy
Here is a small, realistic settings file that uses every rule above. Indentation is two spaces per level, and the dashes line up under the list's parent key:
app: name: shopping-list version: 1.2 debug: false server: host: localhost port: 8080 groceries: - apples - rice - bread owner: name: "Sam O." note: "prefers #2 flour"
Reading it back: app.debug is false, server.port is 8080, and groceries is a list of three strings. The quotes around "Sam O." and "prefers #2 flour" keep the period and the hash from confusing the parser.
The Settings That Matter Most
These are the rules worth memorising, because almost every YAML error traces back to one of them:
| Element | How to write it | Common slip |
|---|---|---|
| Key-value pair | key: value (colon + one space) | Missing the space after the colon |
| Nesting | 2 spaces per level, spaces only | Using tabs, or mixing 2- and 4-space indents |
| List item | - item (dash + one space) | Dashes at inconsistent indent levels |
| String needing quotes | "yes", "1.0", "a: b" | Leading special characters unquoted |
| Comment | # note | Commenting a line without re-checking indentation |
| Booleans / null | true, false, null (bare) | Writing "true" when the program expects a real boolean |
How to Tell Your YAML File Is Valid
The fastest check is an online YAML validator or the editor itself. In VS Code, a .yml file shows red underlines on lines the parser cannot read; hovering the underline names the problem. For a command-line check, Python users can run python -c "import yaml,sys; yaml.safe_load(open('config.yml'))" (with PyYAML installed) — no output means the file parsed cleanly. The real test is the program that consumes it: if Docker Compose or your app starts and reads the values you set, the file is correct.
The One Error Almost Everyone Hits: Indentation
By far the most common failure is a mismatched indent, and it produces errors like "did not find expected key" or "mapping values are not allowed here". The fix: pick two spaces per level, never tabs, and check that every sibling line lines up exactly. If your editor inserts tabs, look for a setting like "insert spaces when pressing Tab" (the exact label differs by editor and version) and turn it on. A quick trick for finding the bad line — delete the blank line above the one the error points at, since a stray space on an "empty" line can also break parsing.
When Do You Need Multi-Line Text in YAML?
Long text blocks use a pipe or a greater-than sign on the key's line. The pipe (|) keeps line breaks exactly as written; the greater-than sign (>) folds lines into one paragraph. Both are indented two spaces:
description: | First line of text. Second line stays on its own line. greeting: > Hello there, this becomes one long line.
Use the pipe for scripts and license text where line breaks matter; use the greater-than sign for prose where they do not.
Does YAML Care About Blank Lines and Order?
Blank lines are ignored, so you can group related sections for readability — the example file above does this. Key order does not matter to the parser either, so you can list server before app without changing anything. What does matter: duplicate keys at the same level are an error in most parsers, so each key name should appear once per block. If two sections need the same key, put them under different parents, like app.name and owner.name.
What YAML Is Not Good For
YAML is built for human-readable configuration, not for very large datasets or documents with heavy formatting. If a file grows past a few hundred lines, comments and clear section grouping matter more than clever shortcuts. For deeply nested data, JSON handles the same structure with braces instead of indentation, and most YAML parsers accept JSON too — so a JSON file is a valid shortcut when nesting gets deep and hand-indenting becomes error-prone.
Official source: YAML.org