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

Popular Backend topics Top

AstonJ
Another BEAM language… The Hamler Programming Language Hamler is a strongly-typed language with compile-time typechecking and built-in...
New
DevotionGeo
For me it’s the semicolon, because I stopped using a semicolon in JavaScript, two other of my favorite languages, Elixir and Go don’t hav...
New
New
Kurisu
Hello and happy new year! I would like to buy a Ruby On Rails ebook for learning purpose. What would be the ROR equivalent of “Programm...
New
Jsdr3398
I really need developers to help create my messaging platform but I’m not sure how much they want etc. I’ve never hired anyone before :s...
New
Cellane
I’ve been asked by my supervisors at work to finally give everyone in the team presentation about “that Elixir thing you can’t seem to sh...
New
ManningBooks
Deep Learning with Python, Second Edition is a comprehensive introduction to the field of deep learning using Python and the powerful Ker...
New
ManningBooks
Rust in Action introduces the Rust programming language by exploring numerous systems programming concepts and techniques. You'll be lear...
New
PragmaticBookshelf
Use WebRTC to build web applications that stream media and data in real time directly from one user to another, all in the browser. ...
New
PragmaticBookshelf
Build fast, scalable PostgreSQL and Rails apps. Solve data growth, quality, and reliability challenges, for workloads from consumer Inter...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1126 25250 746
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New