3

For the following switch block: enter image description here

If I select the switch block and apply the auto format command, the = key, I get the following:

enter image description here

The block is formatted correctly in the first image, but it gets messed up by auto format. It works fine on if blocks, but really messes up the case blocks. What controls the auto-format? Is there any way I can adjust the configuration to apply camel style brackets?

2
  • 1
    Just curious: Those braces are redundant. What benefit do they provide?
    – muru
    Commented Oct 22, 2015 at 3:02
  • @muru There are no advantages, but it does make a difference, so depends on the code intent. Braces control the scope, therefore make it possible to have variables local to the case with braces. For this reason, I consistently add braces, in some cases, when local to the case variable is added, its scope is limited. Commented Oct 22, 2015 at 13:11

1 Answer 1

6

The configuration of C indenting in Vim is controlled by the cinoptions option. That particular aspect of indenting is controlled by the l (lower-case L) flag. When set to any value other than 1, it will "align with a case label instead of the statement after it in the same line."

So, you can get the indenting style you want by putting this in your ~/.vimrc on Unix or ~/_vimrc on Windows:

set cinoptions+=l1

See

:help cino-l
:help cinoptions-values
:help 'cinoption'
:help C-indenting
:help 30.2

Those topics are ordered roughly from particular to general aspects of C indenting.

You can find out more about auto-format here:

:help auto-format

although I don't think that's what you mean by "auto format".

Also, those { and } are called braces. Brackets are the more squarish [ and ].

1
  • after doing some more digging, you are correct, this is the auto-indent, not auto-format. A lot of VIM keyboard cheat sheets call the = key auto-format. Anyway, the l1 option works great... Commented Oct 22, 2015 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.