[erlang-questions] Vim Editor Erlang Indentation

Oscar Hellström <>
Thu Jun 4 19:44:38 CEST 2009


Hi All,

Long answer to short question. Yes, there is a certain level of
rant/flame warning here ;)

I use vim daily in my work, and I also have a lot of opinions on how
Erlang should be indented. I'm using the indent file found here:
http://www.vim.org/scripts/script.php?script_id=2023 and I'm quite happy
with it. It has some minor flaws though. There is a big difference
between the indentation in provided by this script, and the indentation
provided by emacs erlang mode, which has caused much annoyance in the
office. The largest difference is that emacs erlang mode tries to
*align* code, while the indentation script linked above will only
increase or decrease the *level of indentation* depending on what tokens
were found.

We can use creating tables in mnesia as an example... in emacs:
mnesia:create_table(foo,
                    [{attributes,
                      record_info(fields, foo)},
                     {ram_copies, [node()]}]).

While in vim:
mnesia:create_table(foo,
    [{attributes,
            record_info(fields, foo),
        {ram_copies, [node()]}].

In vim I would therefore choose to write this as:
mnesia:create_table(foo, [
        {attributes, record_info(fields, foo)},
        {ram_copies, [node()]}
    ]).

Now, me might argue forever what is the most pretty, or the one that is
easiest to read etc., but the problem I have with the aligning approach
is that the editor has no idea about the difference between indentation
levels and alignment of text. For instance, imagine we use tab
characters for indentation, the whole idea here being that I can choose
to have however many columns per tab character in my editor, and you can
have however many columns you want, and the code would still indent
properly. With aligning this doesn't work since the aligning isn't bound
to be one complete tab character, This causes the editor to insert some
spaces at some point. Even worse, if you're aligning with an open brace
for a function call (as above), the number of characters in the
module/function name has nothing to do with the number of tab characters
you would like to insert. So, to be able to align something properly,
the editor would have to consider indentation levels and alignment. It
would then insert as many tabs as you want for your level of indentation
and spaces for the rest. With the emacs erlang mode I guess you would
increase the indentation level by one for every -> you found, but I'm
not sure if that's true, since I don't use it. With vim, I can choose to
align things, by adding spaces, not tabs. The only problem comes no the
next line, where the script believes that wherever the previous line is,
is my indentation level.

The default setting in emacs is to represent every 8 columns with a tab
character. The erlang mode moves 4 columns / indentation level. This
means that if you move one level, you'll insert 4 spaces, and if the
next line moves another level, you'll have a tab there. I guess you can
see where this goes wrong if you open the file in another editor, which
uses 4 columns / tab character. Also, the aligning will insert some
tabs, and then some spaces... which causes lots of pain. The way we deal
with it here is to only use spaces. I think this is sad though. The
different "styles" of indentation is currently just allowed to exist.

Felipe Kober wrote:
> Hello everyone,
> I like vim editor, but indented output is not the same quality as my
> colleagues in emacs. why is this? is the limitation in vim or vim indent
> file?
>
> thank you,
>
> Felipe.
>
>   
Best regards

-- 
Oscar Hellström, 
Office: +44 20 7655 0337
Mobile: +44 798 45 44 773
Erlang Training and Consulting Ltd
http://www.erlang-consulting.com/



More information about the erlang-questions mailing list