Esc
Start typing to search...

Formatter

keel-fmt is the official code formatter for Keel. It parses source files and pretty-prints them with consistent style, preserving all comments and semantic meaning.

Installation

Install keel-cli and use keel fmt:

cargo install --git https://codeberg.org/Keel/keel-cli

Standalone

Build from source:

cargo build -p keel-fmt

Or install via Nix:

nix develop  # keel-fmt is included in the dev shell

Usage

Format files in-place

keel fmt src/*.kl

Check formatting without modifying

keel fmt --check src/*.kl

Exits with code 1 if any file is not formatted. Useful for CI pipelines.

keel fmt --stdout src/*.kl

Read from stdin

echo 'let   x  =  42' | keel fmt

When no files are given, keel fmt reads from stdin and writes to stdout.

Custom line width

keel fmt --width 80 src/*.kl

CLI Reference

FlagDefaultDescription
<FILES>stdinFiles to format (reads stdin if omitted)
--checkoffCheck if files are formatted, exit 1 if not
--stdoutoffPrint to stdout instead of writing in-place
--width100Maximum line width

Formatting Style

Indentation

keel-fmt uses 4 spaces for indentation (not configurable).

Collections

Short collections (up to 3 elements) stay on one line:

[ 1, 2, 3 ]
{ name = "Alice", age = 30 }

Longer collections use Elm-style leading commas:

[ first
, second
, third
, fourth
]

Pipes

Pipe chains are formatted with one stage per line:

[1, 2, 3, 4, 5]
    |> List.filter (|x| x > 2)
    |> List.map (|x| x * 10)

Comments

All comments are preserved:

  • Line comments (--) are placed after expressions with two-space separation
  • Block comments ({- -}) are preserved above or inline
  • Doc comments ({-| -}) are preserved above declarations

Editor Integration

VS Code / Positron

The keel-vscode extension supports:

  • Format Document with Shift+Alt+F
  • Format on Save (enabled by default, configurable via keel.formatter.formatOnSave)

Zed

The keel-zed-extension supports format on save via keel-fmt.

Helix

Add to .helix/languages.toml:

[[language]]
name = "keel"
formatter = { command = "keel-fmt", args = ["--stdout"] }
auto-format = true

Other Editors

Any editor that supports external formatters can use keel fmt --stdout (or keel-fmt --stdout) as the format command. The formatter reads from stdin when no files are given, making it compatible with standard editor formatting protocols.

Supported Constructs

keel-fmt handles the full Keel language:

  • Declarations: fn, type, type alias, module (including parameterized modules), import
  • Expressions: literals, operators, if/else, case/of, lambdas, pipes, records, tuples, lists, inline expressions
  • Patterns: variables, wildcards, constructors, tuples, lists, records, or-patterns, as-patterns, rest (..), cons (::)
  • Types: function types, generics, records, Maybe, Result, all built-in types
  • Keywords: mut, inline, passing

Properties

  • Idempotent — Running the formatter twice produces identical output
  • Lossless — All comments and semantic meaning are preserved
  • Error-reporting — Clear error messages for lex and parse failures

See Also