I've already read several questions and answers:
- Vim: Use tabs for indentation, spaces for alignment with C source files
- Vim: Align continous lines with spaces
But none of them offers a solution for me.
I really want to apply the "Indent with tabs, align with spaces" principle, but when it comes to auto-indentation, I failed to teach Vim how to do that right.
Consider the code, assuming tabstops == 3
, shiftwidth == 3
(>--
means tab, and .
(a dot) means space):
{
>--long a = 1,
>-->--..b = 2,
>-->--..c = 3;
}
So, it indents with tabs as much as possible, and then fills the rest with spaces. But it is actually a very bad approach: when someone will read this code with different tab size, the code will be messed up. Here what it will look like with tab size equal to 8 chars:
{
>-------long a = 1,
>------->-------..b = 2,
>------->-------..c = 3;
}
It is horrible. The problem is that Vim doesn't distinguish between indentation and alignment.
To make it look correctly with whatever the tab size is, the code should be indented this way:
{
>--long a = 1,
>--.....b = 2,
>--.....c = 3;
}
Then, this code will look nice whatever that tab size is. For example, 8 chars:
{
>-------long a = 1,
>-------.....b = 2,
>-------.....c = 3;
}
How to achieve this?