The tofu fmt command formats OpenTofu configuration files to follow a consistent and canonical style. It applies a portion of the OpenTofu style guide along with other small readability enhancements.
Any OpenTofu commands that generate configuration files will adhere to the formatting enforced by tofu fmt. Therefore, using this command on your own configuration ensures your files align with automatically generated ones for consistency.
Usage
Command:
tofu fmt [options] [target…]
By default, tofu fmt scans the current directory for OpenTofu configuration files. You can specify target:
A directory: fmt will scan files in that directory.
A file: fmt will only process that specific file.
A single dash (-): fmt will read from standard input (STDIN).
Available Options:
-write=false – Do not overwrite original files (implied with -check or when using STDIN).
-list=false – Suppress listing of files with formatting issues.
-diff – Show differences between current and formatted versions.
-check – Verify if files are already formatted. Exit status: 0 if all files are correctly formatted; non-zero if not.
-recursive – Process files in subdirectories. By default, only the specified directory (or current directory) is processed.