yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Update and set style of a particular node (simple)
  • Update and set style of a particular node using path variables
  • Set tagged style
  • Set double quote style
  • Set double quote style on map keys too
  • Set single quote style
  • Set literal quote style
  • Set folded quote style
  • Set flow quote style
  • Reset style - or pretty print
  • Set style relatively with assign-update
  • Read style

Was this helpful?

  1. Operators

Style

The style operator can be used to get or set the style of nodes (e.g. string style, yaml style). Use this to control the formatting of the document in yaml.

Update and set style of a particular node (simple)

Given a sample.yml file of:

a:
  b: thing
  c: something

then

yq '.a.b = "new" | .a.b style="double"' sample.yml

will output

a:
  b: "new"
  c: something

Update and set style of a particular node using path variables

Given a sample.yml file of:

a:
  b: thing
  c: something

then

yq 'with(.a.b ; . = "new" | . style="double")' sample.yml

will output

a:
  b: "new"
  c: something

Set tagged style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '.. style="tagged"' sample.yml

will output

!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
f: !!seq
  - !!int 1
  - !!int 2
  - !!int 3
g: !!map
  something: !!str cool

Set double quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '.. style="double"' sample.yml

will output

a: "cat"
b: "5"
c: "3.2"
e: "true"
f:
  - "1"
  - "2"
  - "3"
g:
  something: "cool"

Set double quote style on map keys too

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '... style="double"' sample.yml

will output

"a": "cat"
"b": "5"
"c": "3.2"
"e": "true"
"f":
  - "1"
  - "2"
  - "3"
"g":
  "something": "cool"

Set single quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '.. style="single"' sample.yml

will output

a: 'cat'
b: '5'
c: '3.2'
e: 'true'
f:
  - '1'
  - '2'
  - '3'
g:
  something: 'cool'

Set literal quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '.. style="literal"' sample.yml

will output

a: |-
  cat
b: |-
  5
c: |-
  3.2
e: |-
  true
f:
  - |-
    1
  - |-
    2
  - |-
    3
g:
  something: |-
    cool

Set folded quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '.. style="folded"' sample.yml

will output

a: >-
  cat
b: >-
  5
c: >-
  3.2
e: >-
  true
f:
  - >-
    1
  - >-
    2
  - >-
    3
g:
  something: >-
    cool

Set flow quote style

Given a sample.yml file of:

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

then

yq '.. style="flow"' sample.yml

will output

{a: cat, b: 5, c: 3.2, e: true, f: [1, 2, 3], g: {something: cool}}

Reset style - or pretty print

Set empty (default) quote style, note the usage of ... to match keys too. Note that there is a --prettyPrint/-P short flag for this.

Given a sample.yml file of:

{a: cat, "b": 5, 'c': 3.2, "e": true,  f: [1,2,3], "g": { something: "cool"} }

then

yq '... style=""' sample.yml

will output

a: cat
b: 5
c: 3.2
e: true
f:
  - 1
  - 2
  - 3
g:
  something: cool

Set style relatively with assign-update

Given a sample.yml file of:

a: single
b: double

then

yq '.[] style |= .' sample.yml

will output

a: 'single'
b: "double"

Read style

Given a sample.yml file of:

{a: "cat", b: 'thing'}

then

yq '.. | style' sample.yml

will output

flow
double
single
PreviousString OperatorsNextSubtract

Last updated 1 year ago

Was this helpful?