DevotionGeo

DevotionGeo

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).

Most Liked

AstonJ

AstonJ

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:

DevotionGeo

DevotionGeo

You’re welcome and thank you! :slight_smile:

DevotionGeo

DevotionGeo

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]}.

Where Next?

Popular Backend topics Top

Scorpil
I dabbled in Phoenix for a while now, but never really got my hands dirty with it right up until now. Apart from the whole framework bein...
New
First poster: bot
Part 1: Introduction to Postgrest. In Codd, we trust In the field of Computer Science and Engineering, few things come close to the dura...
New
First poster: bot
It’s not legacy code — it’s PHP. Vimeo has been using PHP in production for over 15 years. Find out how we keep a million lines of PHP i...
New
First poster: bot
Django 3.2 is just around the corner and it’s packed with new features. Django versions are usually not that exciting (it’s a good thing!...
New
First poster: Exadra37
Summary: I describe a simple interview problem (counting frequencies of unique words), solve it in various languages, and compare perform...
New
First poster: bot
This post explains why Scala projects are difficult to maintain. Scala is a powerful programming language that can make certain small te...
New
First poster: bot
At Grammarly, the foundation of our business, our core grammar engine, is written in Common Lisp. It currently processes more than a thou...
New
First poster: bot
Episode 299 - 10 Tips and Tricks. I don’t get around to doing these too often, but they are always a lot of fun. In this episode, we’ll ...
New
wolf4earth
Charles Max Wood takes the lead this week. He and Adi Iyengar discuss what Top End Devs are and what people should be doing to become Top...
New
chikega
Mark Hoffman, the author of Programming WebAssembly in Rust, is a pretty hilarious lecturer if you like a dry sense of humor.
New

Other popular topics Top

AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New