Coloring defined types as types

Issue

Is there any way to add syntax coloring to new types defined with typedef statements in C?

typedef struct {
    int a,b;
} MyStruct;

MyStruct *InitMyStruct(MyStruct *struct, int a, int b);
    ^         ^           ^               ^      ^
    +---------+-----------+               +------+
    Same Color                             Correct type color

If it’s not possible natively (I guess so), are there any plugins to make this visual clue work?

Solution

I found the exact solution to my question in Vim’s help, and I’m posting it here in case someone needs this in the future. It’s exactly what I want: a way to read the code and highlight it accordingly.

syntax.txt

Section 15: Highlighting tags

[...]
Only highlighting typedefs, unions and structs can be done too.  For this you
must use Exuberant ctags (found at http://ctags.sf.net).

Put these lines in your Makefile:

# Make a highlight file for types.  Requires Exuberant ctags and awk
types: types.vim
types.vim: *.[ch]
        ctags --c-kinds=gstu -o- *.[ch] |\
                awk 'BEGIN{printf("syntax keyword Type\t")}\
                        {printf("%s ", $$1)}END{print ""}' > $@

And put these lines in your .vimrc: >

   " load the types.vim highlighting file, if it exists
   autocmd BufRead,BufNewFile *.[ch] let fname = expand('<afile>:p:h') . '/types.vim'
   autocmd BufRead,BufNewFile *.[ch] if filereadable(fname)
   autocmd BufRead,BufNewFile *.[ch]   exe 'so ' . fname
   autocmd BufRead,BufNewFile *.[ch] endif

Answered By – sidyll

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.