How to Write YAML Front Matter Between Lines of Three Dashes
YAML front matter is a block of YAML settings at the very top of a file, between two lines of three dashes. Everything below the second line of dashes is the file's own content, usually a Markdown page.
This guide writes front matter at the top of a Markdown page, reads it and changes it with yq 4.54.3, and shows the three mistakes that stop yq from finding it.
Syntax
---
<key>: <value>
<key>: <value>
---
<the rest of the file>
| Part | What it means |
|---|---|
---, the first one | Three dashes, alone on the file's first line: the front matter starts here |
<key>: <value> | One setting per line, in YAML: a key, a colon, a space and the value |
---, the second one | Three dashes alone on a line: the front matter ends here |
<the rest of the file> | The file's own content, such as a Markdown page. It isn't YAML |
The dashes are YAML's own marker. The YAML 1.2.2 specification says: YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present. The front matter is that document, and the second line of dashes is where it stops. yq's documentation calls what follows the non-yaml content block.
The Example
garden.md is a short Markdown page about a vegetable garden. Its front matter holds three settings: the page's title, the number of garden beds, and whether the page is still a draft.
Prerequisites
- yq, to read and change the front matter. This guide uses yq 4.54.3:
yq --versionprintedyq (https://github.com/mikefarah/yq/) version v4.54.3(Install yq).
Step 1: Write the Front Matter
Create garden.md with this text:
---
title: My Vegetable Garden
beds: 3
draft: false
---
# My Vegetable Garden
Tomatoes in the first bed, beans in the second and carrots in the third.

The file opens with --- on its first line. The three settings below it are ordinary YAML, one per line. The second --- closes the front matter, and the page itself follows: a heading and a sentence.
Step 2: Read the Front Matter
yq --front-matter=extract '.' garden.md
---
title: My Vegetable Garden
beds: 3
draft: false
--front-matter=extract makes yq read only the front matter. The yq documentation: Running with --front-matter=extract will only output the yaml contents and ignore the rest. The '.' after it, a single dot, is the expression yq runs on what it read, and here it printed all of it: a --- line, as in the documentation's own example, then the three settings. The closing --- and the page were left out.
Step 3: Read One Setting
yq --front-matter=extract '.title' garden.md
My Vegetable Garden
.title is a path to one key: a dot, then the key's name. yq printed that key's value on its own, without the key. The yq documentation on paths like this: It is used to navigate deeply into yaml structures.
Step 4: Change a Setting and Keep the Page
yq --front-matter=process '.beds = 4' garden.md
---
title: My Vegetable Garden
beds: 4
draft: false
---
# My Vegetable Garden
Tomatoes in the first bed, beans in the second and carrots in the third.
--front-matter=process runs the expression on the front matter and prints the whole file. The documentation: Use --front-matter=process to process the front matter, that is run the expression against the yaml content, and output back the entire file, included the non-yaml content block. The expression .beds = 4 sets beds to 4. Both lines of dashes, the heading and the sentence came out as they were.

yq printed the changed page, but it didn't change the file. grep beds garden.md prints the lines of the file that contain beds:
grep beds garden.md
beds: 3
The yq commands work unchanged in PowerShell: the .title and .beds = 4 runs printed the same output there.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No closing ---: yq --front-matter=extract '.' no-end.md | Error: bad file 'no-end.md': go-yaml load error in scanner (while scanning a simple key) at L8.C1-L9.C1: could not find expected ':' | Close the front matter with --- on a line of its own |
A blank line above the first ---: yq --front-matter=extract '.title' late-start.md | It printed null | Put the first --- on the file's very first line |
Leaving out --front-matter: yq '.beds = 4' garden.md | The heading # My Vegetable Garden and the blank lines were missing from the output | Add --front-matter=process |
Without the closing dashes, yq read the whole of no-end.md as YAML. The heading on line 6 didn't stop it, because in YAML a line that starts with # is a comment: An explicit comment is marked by a “#” indicator. The sentence on line 8 isn't a setting, though, so yq stopped there, looking for a colon.
With a blank line first, the file no longer starts with ---, and yq found no title: it printed null.
Without --front-matter, yq read the whole page as YAML. The second --- started a second YAML document, in which the heading is a comment, and this is what came back:
---
title: My Vegetable Garden
beds: 4
draft: false
---
Tomatoes in the first bed, beans in the second and carrots in the third.
FAQ
How do I save the change in the file?
Add -i. The yq documentation uses it under Update a yaml file, in place:
yq -i --front-matter=process '.beds = 4' garden.md
cat garden.md
---
title: My Vegetable Garden
beds: 4
draft: false
---
# My Vegetable Garden
Tomatoes in the first bed, beans in the second and carrots in the third.
With -i, yq printed nothing and wrote the change into garden.md: cat shows beds: 4, and the page is unchanged.
Can the page use three dashes further down?
Yes. rule.md is garden.md with another --- line further down the page and one more sentence below it. yq --front-matter=extract '.' rule.md printed the same three settings as for garden.md, and process left the later --- as it was:
yq --front-matter=process '.beds = 4' rule.md
---
title: My Vegetable Garden
beds: 4
draft: false
---
# My Vegetable Garden
Tomatoes in the first bed, beans in the second and carrots in the third.
---
Next year: potatoes.
Conclusion
garden.md opens with ---, keeps title, beds and draft in YAML, and closes them with a second --- before the page. yq read My Vegetable Garden from it, changed beds from 3 to 4, and kept the page as it was. Put the first --- on the very first line, close the block with a second one, and give yq --front-matter.
Additional Resources
- YAML 1.2.2 specification, 2.2 Structures: what three dashes mean in YAML.
- yq documentation: Front Matter:
--front-matter=extractand--front-matter=process. - yq documentation:
-iand the other ways to read and update a file.
