tofu fmt tidies your OpenTofu files: it fixes the indentation, lines up the = signs and puts everything in OpenTofu’s standard style. This guide shows how to format OpenTofu files with tofu fmt, how to check them without changing anything, and how to see exactly what would change.
It’s a companion to Part 2 of the OpenTofu Tutorial, where tofu fmt checks your first file.
Cost: free. Everything here runs on your own computer.
Every command and output on this page is real, run on OpenTofu 1.13 on macOS. The commands are the same in Windows PowerShell and on Linux.
Prerequisites
- OpenTofu installed: How to Install OpenTofu on macOS.
- A folder with a
.tffile.tofu fmtdoesn’t needtofu initfirst.
Step 1: Start with a Messy File
Here’s the Bookshop block from Part 2, typed carelessly into main.tf. One line has no indent, the other has too much, and the = signs don’t line up:
resource "local_file" "home_page" {
filename = "index.html"
content = "<h1>Bookshop</h1>"
}
OpenTofu reads it fine, but it’s harder for people to read, and every tidy-up shows up as noise in code reviews.
Step 2: Check the Formatting
-check reports which files need formatting, without changing them:
tofu fmt -check
main.tf
It lists main.tf and ends with an error status, so a check like this can fail a CI/CD pipeline when someone forgets to format.
Step 3: See What Would Change
Add -diff to see the exact changes, still without writing anything:
tofu fmt -check -diff

main.tf
--- old/main.tf
+++ new/main.tf
@@ -1,4 +1,4 @@
resource "local_file" "home_page" {
-filename = "index.html"
- content = "<h1>Bookshop</h1>"
+ filename = "index.html"
+ content = "<h1>Bookshop</h1>"
}
Lines starting with - are the current version, and lines starting with + are what tofu fmt would write: both settings indented by two spaces, with their = signs in one column.
Step 4: Format the Files
Run it without options to fix the files:
tofu fmt
main.tf
It prints the name of each file it changed. main.tf now reads:
resource "local_file" "home_page" {
filename = "index.html"
content = "<h1>Bookshop</h1>"
}
Run tofu fmt again and it prints nothing, because there’s nothing left to fix.
Which Files tofu fmt Formats
- Files ending in
.tf,.tofuand.tfvars, plus OpenTofu test files (.tftest.hcland.tofutest.hcl). - Only the current folder. Add
-recursiveto include every folder below it, which matters once a project has modules.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting -check to fix files | Nothing changes: -check only reports. | Run tofu fmt without options to fix them. |
| Formatting only the top folder | Files in subfolders stay messy. | Use tofu fmt -recursive. |
| Skipping it after an upgrade | OpenTofu’s documentation says the standard style can change slightly between versions. | Run tofu fmt again after upgrading OpenTofu. |
FAQ
Does tofu fmt change what my configuration does?
No. It only changes the layout: spacing, indentation and alignment. The same plan comes out before and after.
Can my editor run tofu fmt for me?
Yes. The official OpenTofu extension for Visual Studio Code formats files with tofu fmt, and can do it every time you save.
Conclusion
tofu fmt puts your files in OpenTofu’s standard style. Use tofu fmt -check to find untidy files, add -diff to see the fixes, and run plain tofu fmt to apply them. Make it a habit before every commit.
Related
- OpenTofu Tutorial — Part 2: Your First OpenTofu Project: init, plan, apply and destroy
- What Does tofu init Do in OpenTofu?
Additional Resources
- Command: fmt (OpenTofu docs)
- Style Conventions (OpenTofu docs)
OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.


Leave a Reply