A lightweight TOML formatter that aligns values and provides optional indentation for improved readability.
I wanted pretty TOML. I wanted Go.
go-pretty-toml is a command-line utility that formats TOML files with consistent alignment and optional indentation. It preserves all data values while making your configuration files more readable and maintainable, but does not preserve comments.
Key features:
- Aligns values for clean, readable formatting
- Optional two-space indentation
- Sorts keys alphabetically
- Preserves data types
- Handles nested tables and array tables properly
- In-place file editing or stdout output
go install github.com/esacteksab/go-pretty-toml@latestFormat a file and print to stdout:
toml-fmt config.tomlFormat a file with indentation:
toml-fmt -i config.tomlFormat a file in-place (overwrite original):
toml-fmt -w config.tomlFormat from stdin:
cat config.toml | toml-fmt-w, --write: Write result back to source file instead of stdout-i, --indent: Indent output using two spaces-h, --help: Show help
# This is a TOML document
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00
[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }
[servers]
[servers.alpha]
ip = "10.0.0.1"
role = "frontend"
[servers.beta]
ip = "10.0.0.2"
role = "backend"title = "TOML Example"
[database]
data = [["delta", "phi"], [3.14]]
enabled = true
ports = [8000, 8001, 8002]
[database.temp_targets]
case = 72
cpu = 79.5
[owner]
dob = 1979-05-27T07:32:00-08:00
name = "Tom Preston-Werner"
[servers]
[servers.alpha]
ip = "10.0.0.1"
role = "frontend"
[servers.beta]
ip = "10.0.0.2"
role = "backend"title = "TOML Example"
[database]
data = [["delta", "phi"], [3.14]]
enabled = true
ports = [8000, 8001, 8002]
[database.temp_targets]
case = 72
cpu = 79.5
[owner]
dob = 1979-05-27T07:32:00-08:00
name = "Tom Preston-Werner"
[servers]
[servers.alpha]
ip = "10.0.0.1"
role = "frontend"
[servers.beta]
ip = "10.0.0.2"
role = "backend"Warning
The formatter currently does not preserve comments in TOML files. Any comments in the source file will be removed during formatting.
The formatter:
- Parses TOML into a structured map
- Categorizes keys into simple key-value pairs, tables, and array tables
- Sorts keys alphabetically within each category
- Formats each section with proper alignment and indentation
- Writes the formatted output
go-pretty-toml can be integrated into your CI/CD pipeline to enforce consistent TOML formatting. For example, with GitHub Actions:
- name: Format TOML files
run: |
go install github.com/esacteksab/go-pretty-toml@latest
find . -name "*.toml" -exec toml-fmt -w -i {} \;MIT License