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
  • Color
  • Pretty Print
  • Indent
  • Unwrap scalars

Was this helpful?

  1. Usage

Output format

Flags to control yaml and json output format

PreviousWithNextWorking with CSV, TSV

Last updated 2 years ago

Was this helpful?

These flags are available for all yq commands.

Color

By default, yq prints with colours if it detects a terminal. You can manually change this by using either

The --colors/-C flag to force print with colors.

The --no-colors/-M flag to force print without colours

Pretty Print

To print out idiomatic yaml use the --prettyPrint/-P flag. Note that this is shorthand for using the operator ... style=""

Indent

Use the indent flag --indent/-I to control the number of spaces used for indentation. This also works for JSON output. The default value is 2.

Note that lists are indented at the same level as the map key at indent level 2, but are more deeply indented at indent level 4 and greater. This is (currently) a quirk of the underlying .

Given:

apples:
  collection:
  - name: Green
  - name: Blue
  favourite: Pink Lady

Then:

yq -I4 sample.yaml

Will print out:

apples:
    collection:
      - name: Green
      - name: Blue
    favourite: Pink Lady

This also works with json

yq -j -I4 sample.yaml

yields

{
    "apples": {
        "collection": [
            {
                "name": "Green"
            },
            {
                "name": "Blue"
            }
        ],
        "favourite": "Pink Lady"
    }
}

Unwrap scalars

By default scalar values are 'unwrapped', that is only their value is printed (except when outputting as JSON). To print out the node as-is, with the original formatting an any comments pass in --unwrapScalar=false

Given data.yml:

a: "Things" # cool stuff

Then:

yq --unwrapScalar=false '.a' data.yml

Will yield:

"Things" # cool stuff

where as without setting the flag to false you would get:

Things
style
yaml parser