LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

tofu-fmt

Format OpenTofu configuration files

TLDR

Format current directory
$ tofu fmt
copy
Format recursively
$ tofu fmt -recursive
copy
Check formatting (exit non-zero if not formatted)
$ tofu fmt -check
copy
Show diff of formatting changes
$ tofu fmt -diff
copy
Format from stdin and write to stdout
$ cat main.tf | tofu fmt -
copy

SYNOPSIS

tofu fmt [options] [target]

DESCRIPTION

tofu fmt rewrites OpenTofu configuration files to a canonical format and style. It applies a subset of the HCL language style conventions, along with other minor adjustments for readability. OpenTofu is the open-source fork of Terraform maintained by the Linux Foundation.By default, tofu fmt scans the current directory for configuration files. If you provide a target directory, it will scan that directory. If a single file is provided, only that file is processed. A hyphen (-) as target reads from standard input.

PARAMETERS

-list=false

Don't list the files containing formatting inconsistencies.
-write=false
Don't overwrite the input files (implied by -check or when reading from stdin).
-diff
Display diffs of formatting changes.
-check
Check if the input is formatted. Exit status is 0 if all input is properly formatted; non-zero otherwise.
-recursive
Also process files in subdirectories. By default, only the given directory is processed.

INSTALL

sudo dnf install opentofu
copy
sudo pacman -S opentofu
copy
sudo apk add opentofu
copy
sudo zypper install opentofu
copy
brew install opentofu
copy
nix profile install nixpkgs#opentofu
copy

CAVEATS

The command modifies files in place unless -check, -write=false, or stdin input is used. Formatting changes from tofu fmt may not be stable across major versions; re-run after upgrades. Syntactically valid but unformatted files are changed; invalid files produce errors without modification.

HISTORY

OpenTofu was forked from Terraform in 2023 after HashiCorp changed Terraform's license to the Business Source License. The fmt subcommand was inherited from Terraform and remains largely compatible with terraform fmt.

SEE ALSO

Copied to clipboard
Kai