less than 1 minute read

I’m a sucker for nicely formatted code and config. Working a lot with Kubernetes I like to see my Kubernetes YAML formatted consistently. This helps find the pieces you’re looking for, and even helps reduce errors.

Here is a command line using the excellent yq tool to format and sort your YAML:

yq --inplace --prettyPrint --indent 4 'sort_keys(..)' <yaml file>

zsh function for your .zshrc file:

# Tidy a given YAML file in place
# Requires recent version of yq
yamltidy() {
  if [ -z $1 ]
  then
    echo Give me a filename dummy
  else
    if [ -f $1 ]
    then
      echo Tidying $1
      yq --inplace --prettyPrint --indent 4 'sort_keys(..)' $1
    else
      echo File $1 not found, nothing to do
    fi
  fi
}