LinuxCommandLibrary

brittany

Format Haskell source code

TLDR

Format a Haskell source file and print the result to stdout

$ brittany [path/to/file.hs]
copy

Format all Haskell source files in the current directory in-place
$ brittany --write-mode=inplace [*.hs]
copy

Check whether a Haskell source file needs changes and indicate the result through the program's exit code
$ brittany --check-mode [path/to/file.hs]
copy

Format a Haskell source file using the specified amount of spaces per indentation level and line length
$ brittany --indent [4] --columns [100] [path/to/file.hs]
copy

Format a Haskell source file according to the style defined in the specified configuration file
$ brittany --config-file [path/to/config.yaml] [path/to/file.hs]
copy

SYNOPSIS

brittany [OPTIONS] [files…]

PARAMETERS

-i, --inplace
    Modify input files directly instead of writing to stdout

-c, --check-mode
    Exit with error if files need reformatting (for CI)

-v, --verbose
    Increase output verbosity for debugging layout

--indent=INT
    Set base indentation level (default: 4)

--columns=INT
    Maximum line length (default: 100)

--disable=NAME
    Disable specific formatting feature

--enable=NAME
    Enable specific formatting feature

--config-file=FILE
    Use custom YAML config file

--help
    Show full help and exit

--version
    Print version info

DESCRIPTION

Brittany is a fast, reliable formatter for Haskell source code, designed to produce consistent, readable layouts while preserving developer intent where possible. It uses a parser-printer approach based on GHC's parser, ensuring it handles all valid Haskell syntax accurately.

Unlike simpler formatters, Brittany offers extensive customization via command-line flags or a YAML config file (.brittany.yaml), allowing control over indentation, line lengths, and extensions like pattern synonyms or type applications. It supports stdin/stdout for piping and can process multiple files or directories.

Key strengths include performance on large codebases, check-mode for CI integration to enforce formatting without changes, and verbose output for debugging layout decisions. Brittany aims for "libre" formatting—highly configurable to match team preferences—making it popular in Haskell projects seeking reproducible builds and clean diffs.

Installation typically via Stack (stack install brittany) or Cabal. It's an active open-source project on GitHub.

CAVEATS

May reflow comments unpredictably; not all GHC extensions perfectly supported. Always test on codebase. Config merging can be tricky with multiple sources.

CONFIGURATION

Project settings via .brittany.yaml in repo root or ~/.config/brittany.yaml. Supports hierarchy: global < project < local.

INPUT MODES

Reads from files, directories (recursive), or stdin (cat file.hs | brittany). Processes *.hs, *.lhs by default.

HISTORY

Developed by Lennart Spitzner starting in 2016 as a response to limitations in tools like hindent. Reached v1.0 in 2020 with stable parser-printer core. Actively maintained, with focus on GHC compatibility up to 9.x.

SEE ALSO

ormolu(1), hindent(1), stylish-haskell(1)

Copied to clipboard