Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indenting code by bracket column index in Vim?

I'm using Vim to edit Erlang code. I'm used to (most Erlang programmers do this) to indent Erlang code by the bracket scope they're in. For example, C is often indented one tab width inside curly brackets:

int main(void) {
    printf("hello, world\n");
    return 0;
}

In Erlang, it's common to indent based on the column where the bracket started:

?assertError({bad_options, [{foo, bar},
                            bad_option]},
             lhttpc:request("http://localhost/", get, [], <<>>, 1000,
                            [bad_option, {foo, bar}])).

(Example above is indented to get the point across, not according to subjective beauty).

Tab width would be used if the block is started on a new line:

?assertError(
    {bad_options, [{foo, bar}, bad_option]},
    lhttpc:request(
         "http://localhost/", get, [], <<>>, 1000,
         [bad_option, {foo, bar}]
    )
).

Relevant parts of my .vimrc:

set expandtab " Spaces for tabs "
set tabstop=4 " Tab width 4 "
set shiftwidth=4
set smarttab
set autoindent

" Enable filetype plugin "
filetype plugin on
filetype indent on

Is there a way to perform this indentation in Vim, and if so, how?

like image 382
Adam Lindberg Avatar asked Sep 07 '11 08:09

Adam Lindberg


2 Answers

There is a fork of vimerl which implements "context aware indentation" instead of "static indentation": https://github.com/aszlig/vimerl.git

Seems to work, so I'll roll with that for a while.

like image 67
Adam Lindberg Avatar answered Sep 20 '22 14:09

Adam Lindberg


This looks you need delve into the murky world of cindent and cinoptions. I believe that putting the following in your vimrc will partially meet your needs:

set cindent
set cinoptions+=(0

But the question is how this will affect other behaviour. See help cinoptions-values for much more information. It should possible to achieve precisely what you want, but it might take some experimenting.

Hope this helps.

like image 31
Prince Goulash Avatar answered Sep 22 '22 14:09

Prince Goulash