YAML Viewer, Formatter & Validator · Tutorial
How to clean up a messy YAML config file
5 min readbeginnerUpdated
Config files that several people have edited end up with deep, uneven indentation, inline lists and odd spacing. This tutorial shows how to use the YAML Formatter to rewrite such a file in one tidy style, expand inline lists into one item per line and keep every comment, so the next review is quick.
What you need
- The YAML Formatter
- A YAML file to tidy, or the sample from the Try it button
Steps
Paste or open the file
Paste the YAML into the left box, or drag the .yaml file onto the drop area. The sample is a deployment file where
servicesis indented by six spaces, the values under each service by nine, and lists are written inline like[emails, invoices,thumbnails].Pick an indent size
Choose 2 spaces in the Indent menu. Two spaces is the most common style for deployment files, and the whole document is re-indented to match: every level is now exactly two spaces deeper than its parent.
Expand inline lists and maps
Tick Expand inline lists. Flow-style collections such as
{NODE_ENV: production, LOG_LEVEL: warn}become one key per line, and[emails, invoices, thumbnails]becomes a list with one dash per item. This makes later edits produce small, clear diffs.queues: [emails, invoices,thumbnails] queues: - emails - invoices - thumbnailsCheck that comments came along
Look at the right box. The heading comment is still at the top, and the
# percentnote that followed the first alert is now on its own line inside that alert, next tothreshold. Nothing is deleted when the layout changes.Optionally sort keys
Tick Sort keys if your team prefers alphabetical order. It is handy before comparing two versions of a file, but leave it off when the order carries meaning for readers, such as
namebeforeimage.Copy or download the result
Press Copy and paste the result back into your editor, or press Download to save
formatted.yaml. Run your usual deploy or lint step to confirm everything still works.
Try it yourself
Opens the YAML Viewer, Formatter & Validator with this tutorial's sample loaded.
Common mistakes
- Tabs in the indentation
- YAML does not allow tab characters for indentation. If you see an error about tabs, replace them with spaces at the line shown, then the formatter can continue.
- Expecting it to fix wrong nesting
- The formatter keeps your structure. If a key sits at the wrong level, it stays at that level, now with neat indentation. Check the nesting yourself, or use the Validate tab.
- Sorting a file where order matters
- Some tools read keys in order, for example step lists written as mappings. Keep Sort keys off for those files.
Next steps
Go back to the YAML Viewer, Formatter & Validator, or keep going with one of these:
FAQ
Will it change quoted strings?
No. A value written as "bento/web:2.4.1" keeps its quotes, and unquoted values stay unquoted.
Can I format several documents at once?
Yes. Paste them together with --- between them and each one is formatted in turn.