Anne van Rossum
Anne van Rossum

Reputation: 3149

How to indent in vim for designated initializers?

How to do proper indentation in vim for designated initializers of structs (in C)?

This is how it is indented by default:

typedef struct {
    int foo;
    int bar;
} child_t;

typedef struct {
    child_t child;
} parent_t;

int main(int argc, char *argv[]) {
    parent_t p;
    p.child = (child_t) {
        .foo = 1,
            .bar = 2,
    };
}

How can I have it indented like this?

typedef struct {
    int foo;
    int bar;
} child_t;

typedef struct {
    child_t child;
} parent_t;

int main(int argc, char *argv[]) {
    parent_t p;
    p.child = (child_t) {
        .foo = 1,
        .bar = 2,
    };
}

I've tried quite a few options from https://vimdoc.sourceforge.net/htmldoc/indent.html but haven't been able to make it work.

Removing (child_t) allows it to do indent properly, but in that case the gcc compiler complains with an expected expression before ‘{’ token error message (because apparently it isn't able to infer what type p.child is.

Upvotes: 0

Views: 108

Answers (1)

leongross
leongross

Reputation: 427

If you use vim and want propper formatting, I can recomd to use coc vim and ccls. Further you can use clang-format to format your source files propperly.

Upvotes: 0

Related Questions