Can I use the formatter to indent with three spaces?

I see that Elixir now comes with a .formatter.exs file:

# Used by "mix format"
 inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]

How can I adjust this file so that it formats my Elixir files using three spaces for an indent?

You cannot no. The purpose of the formatter is to produce standardized code formatting across the community, which would be undercut if massive things like spacing were configurable.


My intention was to do this only for my own local environment – I would just use a different formatter when pushing up to GitHub, etc.

Sadly it doesn’t go far enough. There are things where the formatter allows you to do whatever, like various newlines between @spec, @doc, a function head (without a body; where you can specify default argument values) and the functions bodies themselves. I had other examples in the past but sadly never wrote them down.

I remember having CI fail on me just a few days ago because after a refactor there was an extra newline between @spec and a function body which I trusted mix format to fix but it didn’t. :frowning:

So if anything, I’d celebrate a complete dictator Elixir formatter!

Rust does this really well. When I contracted for a few companies with it I was kind of bummed out initially – some code got reformatted really weirdly for my taste – but their devs convinced me that they prefer to not fiddle with rustfmt configuration (because it can be huge if you go down that rabbit hole). I ended up getting used to the enforced format and even liking it because it was designed to make the (sometimes very cryptic) Rust code easier to read. It took some getting used to though, but that’s another discussion entirely – Rust code can get very weird looking.

1 Like