It seems there are many options in Vim for indenting c code with the cindent and cinoptions variables. I've made no modifications to these settings and I've tested this with a fresh install of Vim and with no vimrc. I can't seem to find an explanation for the following behavior:
If public is used on the first line in the class body, it appears to format the indentation correctly. Using gg=G formats the body like so:
class Thing
{
public:
int a;
};
But, if there's something above it, vim does not indent the line after the public keyword. Using gg=G formats the body like so:
class Thing
{
int b;
public:
int a; //WHY is there no additional indentation level anymore???
};
Does anyone know why this occurs? It's driving me nuts.
Thank you!
Installing clang-format and using an autocmd solved my issue. Any new settings that I want to apply to c or c++ files can simply be added to the CPPFormatSettings function. Google's formatting makes the most sense to me, but there are 5 or 6 other styles to choose from. Changing to -style=microsoft will format the code to look more like classic C++ formatting with the brackets below the function definitions.
Here's what I've added to my vimrc:
fun! CPPFormatSettings()
setlocal equalprg=clang-format\ -style=google
endfun
autocmd FileType c,cpp call CPPFormatSettings()