Formatting your Erlang code in Visual Studio Code

There are 3 main formatters for Erlang which you can use from the command-line,

Visual Studio Code’s extension called Erlang Formatter uses these formatters to format Erlang code on save or when the shortcut keys combination is pressed which is Shift + Alt + F on Windows, Shift + Option + F on Mac and Ctrl + Shift + I on Linux.
(Note: the erlang extension also tries to format Erlang code, but it wasn’t working for me, and it doesn’t have any setting to set the desired formatter).

Following were the steps I used to make it work (all the settings and instructions can be found at Github READMEs of Erlang Formatter for VSCode and rebar3_format, and the Getting Started Page of rebar3, but I’m trying to make it a tiny bit less confusing than how I found it).

  1. Install rebar3:
    If you already have Elixir installed, chances are that there is a rebar3 binary somewhere in that installation. Mine was at ~/.asdf/installs/elixir/1.10.4-otp-23/.mix/rebar3 as I use asdf for version management. In that case you can run the following command, replacing the path with the path to rebar3 on your machine, or can go to the directory where rebar3 is located and run as ./rebar3.
    -> ~/.asdf/installs/elixir/1.10.4-otp-23/.mix/rebar3 local install

    Then add export PATH=$PATH:~/.cache/rebar3/bin to your .bashrc or .zshrc. And after that run the second command as,
    -> rebar3 local upgrade
    (this time you don’t need to add ./ or the complete path to the command, as rebar3 (the new copy) is already installed to ~/.cache/rebar3/lib and is added to the path).

    Alternatively you can run the following commands to install from source.
    -> git clone https://github.com/erlang/rebar3.git
    -> cd rebar3
    -> ./bootstrap
    -> ./rebar3 local install
    Don’t forget to add export PATH=$PATH:~/.cache/rebar3/bin to your .bashrc or .zshrc.

  2. Create a rebar.config file under ~/config/rebar3/ or simply run the following comand in the terminal.
    -> code ~/.config/rebar3/rebar.config
    which will open a rebar.config file under ~/.config/rebar3/ in vscode.
    Add the format configuration to this file. You can find the configuration options at the rebar3_format README at Github.
    Following is my rebar.config (notice the comments).

{plugins, [rebar3_format]}.
{format, [
    {files, ["**/*.erl"]},
    {formatter, default_formatter},
    {options, #{
        % paper => 100,
        % inline_qualified_function_composition => true,
        % inline_simple_funs => true,
        % inline_items => all,
        % inline_attributes => all,
        % inline_expressions => true,
        inline_clause_bodies => true
    }}
]}.
  1. Install the VSCode extension Erlang Formatter, if not installed already, and add the following settings to your settings.json.
  "[erlang]": {
    "editor.tabSize": 4,
    "editor.defaultFormatter": "szTheory.erlang-formatter",
    "editor.formatOnSave": true
  },
  "erlangFormatter.formatter": "rebar3_format",

I wanted to have a 2 spaces tab size for Erlang, but looks like it’s a setting of rebar3_format which can’t be overridden, so I set it to 4 spaces in the editor too, so that the indent guides don’t look off.

Edit: There is a setting break_indent, which can be used to have 2 space indents instead of 4.

(Note: feel free to edit this post).

2 Likes

Corresponding tweet for this thread:

Share link for this tweet.

2 Likes

Nice one - thanks for writing this up DG! :+1:

I’ve made it a wiki for you too - if anyone edits it you’ll be sent a notification :nerd_face:

2 Likes

You’re welcome and thank you! :slight_smile:

2 Likes

I changed the Erlang formatter from rebar3_format to erlfmt and it proved to be a lot better than rebar3_format.
The rebar.config contains only one line now, which is {plugins, [erlfmt]}.

2 Likes