Rewriting to Haskell–Automatic Formatting

Posted on March 19, 2020 by Riccardo

Rewriting to Haskell (Series)

Rewriting to Haskell–IntroRewriting to Haskell–Project SetupRewriting to Haskell–DeploymentRewriting to Haskell–Automatic FormattingRewriting to Haskell–ConfigurationRewriting to Haskell–Standing on the shoulders of RailsRewriting to Haskell–Making GHC More NitpickyRewriting to Haskell–TestingRewriting to Haskell–LintingRewriting to Haskell–Parsing Query ParamsRewriting to Haskell–Parsing Query Params, AgainRewriting to Haskell–Errors

One comment in a pull request about code formatting was enough to convince us we needed a tool to solve the problem once for all!

We decided to go with Ormolu because it's really similar to how Elm Format behaves. In particular, from the readme, among other things:

Implementing one “true” formatting style which admits no configuration.


Let some whitespace be programmable. The layout of the input influences the layout choices in the output. This means that the choices between single-line/multi-line layouts in each particular situation are made by the user, not by an algorithm. This makes the implementation simpler and leaves some control to the user while still guaranteeing that the formatted code is stylistically consistent.

To install Ormolu it's enough to run the following:

stack install ormolu

Then formatting can be checked in CI with:

ormolu -m check **/*.hs

Or formatted with:

ormolu -m inplace **/*.hs

Also, plugins are available to automate formatting whenever a Haskell file is saved! In VIM it works really well:

Gif showing Ormolu formatting a Haskell file in a VIM buffer


It's one of the selected few I follow every week – Mateusz

Tired of RELEARNING webdev stuff?

  • A 100+ page book with the best links I curated over the years
  • An email once a week full of timeless software wisdom
  • Your recommended weekly dose of pink
  • Try before you buy? Check the archives.