| name | yq |
| description | Query, transform, and convert YAML files using yq (v4). Use when reading, updating, merging, or converting YAML/JSON/TOML/XML, including front-matter extraction from markdown files. |
yq — YAML Processing
Process YAML (and JSON/TOML/XML) from the command line with Mike Farah's yq v4.
Core Patterns
yq '.key.nested' file.yaml
yq -i '.key = "value"' file.yaml
echo 'foo: bar' | yq '.foo'
yq -n '.name = "demo" | .version = 1'
yq -i '(.a = 1) | (.b = 2)' file.yaml
Common Operations
Get / Set / Delete
yq '.metadata.name' file.yaml
yq -i '.metadata.name = "new"' file.yaml
yq -i 'del(.metadata.annotations)' file.yaml
Arrays
yq '.items[0]' file.yaml
yq -i '.items += ["new"]' file.yaml
yq -i '.items |= map(select(. != "remove"))' file.yaml
yq 'length' file.yaml
Merge
yq '. *= load("override.yaml")' base.yaml
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' a.yaml b.yaml
Conditional Update
yq -i '(.items[] | select(.name == "target")).value = "patched"' file.yaml
Format Conversion
yq -o json file.yaml
yq -p json file.json
yq -p json -o yaml file.json
yq -o toml file.yaml
yq -p toml file.toml
yq -o xml file.yaml
yq -p xml file.xml
Useful Flags
| Flag | Purpose |
|---|
-i | Edit file in place |
-n | Null input — start from scratch |
-o FORMAT | Output format: yaml, json, toml, xml, csv, tsv, props |
-p FORMAT | Parse input as format (same options as -o) |
-P | Pretty print |
-N | No colors |
-e | Exit with error on missing key |
-f FILE | Read expression from file |
--header-preprocess | Pre-process headers (enabled by default) |
--front-matter | Extract/process YAML front-matter (extract, process) |
Expression Syntax
Navigation
.key
.key.nested
.items[0]
.items[-1]
.items[]
.a, .b
Wildcards and Recursion
.items[].name
..
.[] | key
Select / Filter
select(.age > 30)
select(.name == "target")
select(.tags[] == "important")
select(has("optional"))
Assignment
.key = "value"
.key |= . + 1
.key //= "default"
Functions
length
keys
values
type
sort_by(.name)
group_by(.category)
unique
flatten
to_entries
from_entries
env(VAR)
strenv(VAR)
Comments
yq '.key | head_comment' file.yaml
yq '.key | line_comment' file.yaml
yq '.key | foot_comment' file.yaml
yq -i '.key head_comment = "above"' file.yaml
yq -i '.key line_comment = "beside"' file.yaml
KEG Configuration through tap config edit
KEG config output from tap config can be large. Dump it to a temp file, use
yq to manipulate it, then pipe the result back through tap config edit:
tap config > /tmp/keg-config.yaml
yq '.entities' /tmp/keg-config.yaml
yq '.some.setting = "new-value"' /tmp/keg-config.yaml | tap config edit
yq -i '.some.setting = "new-value"' /tmp/keg-config.yaml
cat /tmp/keg-config.yaml | tap config edit
rm /tmp/keg-config.yaml
Front-matter Processing
Extract or update YAML front-matter in markdown files:
yq --front-matter=extract '.title' post.md
yq -i --front-matter=process '.tags += ["new"]' post.md
Built-in Documentation
yq --help
yq eval --help
Upstream docs: https://mikefarah.gitbook.io/yq/