Adapting swich-case indentation

14 views
Skip to first unread message

radl...@gmail.com

unread,
May 27, 2024, 6:06:51 PMMay 27
to vim_use
Is there a way to bend Vim to my will in indenting C switch-case statements with local scopes delimited by braces.  Here's what I mean: (Vim 9.1 on MacOS (M2 chip and Intel x86-64) 
int main() {
    int n = 42;
    switch (n) {
        case 42: {
            int twice = 2*n;
        }        
    }
}

The above example illustrates what I want:.  But when I visual-select those lines and press =. I get this:
int main() {
    int n = 42;
    switch (n) {
        case 42: {
                     int twice = 2*n;
                 }
    }

The compiler insists on the braces because of that local int twice variable;   putting the opening brace of the case: on its own line helps, but then I have a deviation from my preferred style. 

Please note that this example is contrived to illustrate the problem, not an excerpt from  an actual program..

Am I just finally asking too much of Vim?

Eike Rathke

unread,
May 27, 2024, 7:44:54 PMMay 27
to vim...@googlegroups.com
Hi radl...,

On Monday, 2024-05-27 15:06:51 -0700, radl...@gmail.com wrote:

> Is there a way to bend Vim to my will in indenting C switch-case statements
> with local scopes delimited by braces. Here's what I mean: (Vim 9.1 on
> MacOS (M2 chip and Intel x86-64)
> int main() {
> int n = 42;
> switch (n) {
> case 42: {
> int twice = 2*n;
> }
> }
> }

Play with the cinoptions values, see :h 'cinoptions-values'

Adding l1 should suit your case. So
:set cinoptions=l1
if you don't have other values already.

Eike

--
OpenPGP/GnuPG encrypted mail preferred in all private communication.
GPG key 0x6A6CD5B765632D3A - 2265 D7F3 A7B0 95CC 3918 630B 6A6C D5B7 6563 2D3A
Use LibreOffice! https://www.libreoffice.org/
signature.asc
Reply all
Reply to author
Forward
0 new messages