Skip to content

/flags/caddy/caddy-fmt-overwrite

caddy fmt --overwrite and --diff: format a Caddyfile

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v2.11.7 h1:yj0Y4fYZGPkSvibBJ1sTWE33xC0fxztVyXEW5iIdUT4=updated 2026-10-20
tool
Caddy
command
caddy fmt
flag
--overwrite
default
print to standard output

caddy fmt [Caddyfile] reads a Caddyfile and prints it in the canonical format: tabs for indentation and consistent spacing. Without flags it only prints. --overwrite (-w) writes the result back to the file, and --diff prints what would change. When the input is not formatted, caddy fmt exits with a non-zero status, so it can be used as a CI check.

Format a file

bash
printf 'example.com {\n  reverse_proxy localhost:3000\n    encode   gzip\n}\n' > Caddyfile
caddy fmt --diff Caddyfile 2>/dev/null
caddy fmt Caddyfile >/dev/null 2>&1; echo "unformatted, exit status: $?"
caddy fmt --overwrite Caddyfile
cat -A Caddyfile | sed 's/\$$//'
caddy fmt Caddyfile >/dev/null 2>&1; echo "formatted, exit status: $?"

Output

  example.com {
-   reverse_proxy localhost:3000
-     encode   gzip
+ 	reverse_proxy localhost:3000
+ 	encode gzip
  }

unformatted, exit status: 1
example.com {
^Ireverse_proxy localhost:3000
^Iencode gzip
}
formatted, exit status: 0

cat -A shows tabs as ^I: the formatter indents with tabs.

Flags

FlagEffect
(none)print the formatted Caddyfile to standard output
--overwrite, -wwrite the result back to the file
--diffprint a diff instead of the whole file
FILE or -the file to format; - reads standard input (default: ./Caddyfile)

caddy run, caddy validate and caddy adapt also warn when a Caddyfile is not formatted, but they still load it.

Sources