Formatting Erlang code in Visual Studio Code

15 Jan 2023 17:04 vscode erlang rebar3

The ErlangLS extension for VS Code includes formatting, using rebar3_format. I’d prefer to use erlfmt, so here’s how it should be set up.

This is a corrected version of this post.

Install VS Code extension

  • Install the erlang-ls.erlang-ls extension.

Configure the formatter

The ErlangLS extension uses rebar3_format, and this is hard-coded. But you can configure rebar3_format to use a different formatter, instead of its default, built-in one. Here’s how.

To do this globally, create ~/.config/rebar3/rebar.config containing the following:

{plugins, [rebar3_format, erlfmt]}.
{format, [
        {formatter, erlfmt_formatter}
]}.
The --files option to rebar3 format wants to handle its own wildcards, so use quotes: rebar3 format --files 'src/*.erl'