Ross Lagerwall
Ross Lagerwall

Reputation: 93

Indentation of blocks within case statements in vim

I'm trying to get vim to play nicely with GTK's coding style by setting cinoptions but I can't figure out what to use for case statements.

Desired:

switch (x)
  {
  case 1:
    {
      foo();
    }
  case 2:
    bar();
  }

A) What I get with (not desired): setlocal et sw=2 cinoptions=>4,n-2,{2

switch (x)
  {
  case 1:
      {
        foo();
      }
  case 2:
    bar();
  }

B) What I get with (not desired): setlocal et sw=2 cinoptions=>4,n-2,{2,=0

switch (x)
  {
  case 1:
    {
      foo();
    }
  case 2:
  bar();
  }

Is there a way of getting the desired indentation automatically?

Upvotes: 2

Views: 402

Answers (1)

R Sahu
R Sahu

Reputation: 206667

From the vim :help cino-:

                      *cino-:*
    :N    Place case labels N characters from the indent of the switch().
          (default 'shiftwidth').

        cino=                 cino=:0
            switch (x)            switch(x)
            {                     {
               case 1:            case 1:
                  a = b;             a = b;
               default:           default:
            }                     }

                                                   *cino-=*
    =N    Place statements occurring after a case label N characters from
          the indent of the label.  (default 'shiftwidth').

            cino=                 cino==10 >
               case 11:             case 11:  a = a + 1;
                   a = a + 1;                 b = b + 1;

You need add :0 to your cinoptions.

If need to also set =3 if you want 3 characters of indent under case.

Upvotes: 3

Related Questions