How do I format an EK9 source file?
← Code Formatting · Ref: Q622
EK9 has a built-in code formatter, similar to gofmt for Go or rustfmt for Rust. Use the -f flag to format a single file.
BASIC USAGE
ek9 -f myfile.ek9
This parses the file first, then rewrites it in canonical format. The file must be valid EK9 (parseable) for -f to work.
WHAT IT CHANGES
- Normalises indentation to canonical two-space indent.
- Standardises whitespace around operators and keywords.
- Aligns block structure consistently.
- Removes trailing whitespace.
- Ensures consistent blank-line usage between constructs.
WHAT IT PRESERVES
- Comments (both doc-comments and inline comments) are preserved.
- Blank lines that separate logical sections are maintained.
- The semantic meaning of the code is never altered.
WHEN TO USE -f
Use -f after writing or editing a file manually. It is safe to run repeatedly because the output is idempotent: formatting an already-formatted file produces the same file.
See Q623 for formatting all project files. See Q624 for force-formatting unparseable files. See Q625 for why EK9 enforces a standard format. See Q626 for integrating formatting into your workflow. See Q16 for indentation rules.
Example
defines module qa.codeformatting.single defines function <?- A function that would be reformatted by ek9 -f if written with inconsistent spacing. After formatting, indentation is canonical. -?> greet() as pure -> name as String <- message as String: `Hello, ${name}!` calculateArea() as pure -> width as Float height as Float <- area as Float: width * height defines program FormatDemo() stdout <- Stdout() greeting <- greet("World") stdout.println(greeting) roomArea <- calculateArea(4.5, 3.2) stdout.println(`Area: ${roomArea}`)
Common mistakes
E50001 — Removing the variable declaration means later references to the variable become unresolved, triggering E50001. See ek9 -h E50001 for details.
Incorrect:
greet("World")
Correct:
greeting <- greet("World")
Other ways to ask this
- How do I auto-format my EK9 code?
- Does EK9 have a built-in formatter like gofmt?
- How do I canonically format a single .ek9 file?
Coming from another language?
Go: gofmt built into toolchain, universally adopted, single canonical style. Rust: rustfmt (cargo fmt) built into toolchain. Python: black (external, opinionated), autopep8, yapf. Java: google-java-format, spotless (all external plugins). JavaScript: prettier (external). EK9: ek9 -f built into compiler binary, same philosophy as gofmt (one true format, no configuration).
Keywords: migrate, rustfmt, whitespace, format, style, gofmt, indent, auto-format, canonical, formatter