This post describes how to setup and use rebar3_format for Erlang code formatting.

1. Install Erlang if not done already.

brew install erlang

2. Install rebar3.

# download escript version of rebar3
cd ~/temp
curl -o rebar3 https://s3.amazonaws.com/rebar3/rebar3
chmod +x rebar3
./rebar3 -v
rebar 3.23.0 on Erlang/OTP 26 Erts 14.2.5

# compile and install
./rebar3 local install

# add to path
echo -n '# rebar3\nexport PATH=$PATH:~/.cache/rebar3/bin\n' >> ~/.zshrc
source ~/.zshrc

# check version
rebar3 -v
rebar 3.23.0 on Erlang/OTP 26 Erts 14.2.5

3. Add the following to ~/.config/rebar3/rebar.config. Create this file if not present.

{plugins, [rebar3_format]}.

4. Install rebar3_format.

rebar3 format
===> Fetching rebar3_format v1.3.0
===> Fetching katana_code v2.0.2
===> Analyzing applications...
===> Compiling katana_code
===> Compiling rebar3_format

This will install rebar3_format and katana_code under ~/.cache/rebar3/plugins.

5. Enable all features in Erlang VM for OTP25+.

echo -n '# rebar3_format\nexport ERL_AFLAGS="-enable-feature all"\n' >> ~/.zshrc
source ~/.zshrc

6. Format files

# format a single file
rebar3 format --files ~/temp/foo.erl

# format all .erl files in the given directory
rebar3 format --files 'src/*.erl'

# format all files in the current project
rebar3 format