Vim has several indent methods, including
Each one is unique and differently powerful.
:help cino-i, you want to use
'cindent'. This is already set for
cpp files in
$VIMRUNTIME/indent/cpp.vim, as found by doing
:verbose set cindent? from a
Customizing cindent for this case
To change the indentation of "C++ base class declarations and constructor initializations," you then want to change
'cinoptions' to include
The example in the help docs:
iN Indent C++ base class declarations and constructor
initializations, if they start in a new line (otherwise they
are aligned at the right side of the ':').
cino= cino=i0 >
class MyClass : class MyClass :
public BaseClass public BaseClass
MyClass::MyClass() : MyClass::MyClass() :
In order to accomplish that, I would do the following:
- Set up a
C++ filetype plugin by creating the the file
~/.vim/ftplugin/cpp.vim (or wherever your vim directory is)
- Add the following line to it:
This works because
'cinoptions' defaults to an empty string, is not changed by a filetype plugin, and the values used when not provided include
is (use a
'shiftwidth') as seen in the help:
Caveat based on OP's vimrc (AKA How to enable filetype-settings)
The OP's post already has
i0 in it (although it is set globally in all files). However, it does not appear to be working.
In order for this solution (and indeed
cindent in general) to work, you need to enable filetype detection, plugins, and indenting (at the very least). To do this, add the following line to your vimrc:
filetype plugin indent on
You can find some more similar information on this vi.stackexchange post and this one and this one.They're all mine, because they were easy to find and I have written extensively on the topic.
Some testing related the unfortunate experience that the precise snippet in the OP then becomes
Horse(Color color, HANDS height, int age):
cout << "text" ;
which is only a small step towards the solution requested by the OP. This can be corrected by not indenting continuation lines (
'cinoptions'). However, then continuation lines won't be indented (see
:help cino-+). I suppose these are technically continuations (hence the commas, hence the indent).
class MyClass :
cout << "text" ;
A better solution remains to write and use
indentexpr (possibly falling back on
cindent() except in this case). I am not currently up to the task.