I want to use syntax highlighting for custom C types. Based on my coding convention, types can occur in the following places:

typedef type1 type2;

(static | extern | register | volatile)? type ...;

(struct | enum | union) type ...;

func(type1 x, type2 x, ...);  // might be a harder case

It doesn't include all the situations, but can already cover probably more than 90% of the usage. It seems to me like just a simple pattern matching problem, but I was a bit frustrated playing with vim syntax script, and couldn't get what I need. For example, I don't know how to match part of the pattern without highlighting the rest. Any ideas how to do it?


  • \zs and \ze are great for defining the start and end of the matching part of your pattern
    – tommcdo
    Commented Jul 24, 2016 at 21:18
  • @tommcdo It doesn't work for more than one group in the match. For example in the function signature case.
    – R.L
    Commented Jul 24, 2016 at 23:51
  • You just have to get more creative with your regular expression. Look for something like type name following and preceding any number of things like type name. Each parameter will match that same pattern.
    – tommcdo
    Commented Jul 25, 2016 at 0:24

1 Answer 1


The way built-in types are declared is very simple. From /usr/share/vim/vim74/syntax/c.vim:

syn keyword     cType           int long short char void
syn keyword     cType           signed unsigned float double
" [..etc..]

And then later on it sets a highlight group:

hi def link cType               Type

Adding to this syntax group is easy:

:syn keyword cType type1

Or is you want a different highlight group, use something like:

:syn keyword cTypeCustom type1
:hi def link cTypeCustom MyType
:hi MyType ctermfg=darkblue guifg=darkblue

The trick here is to let Vim know which custom types are yours. You could "regex it", but it seems to me that a far better and more robust solution would be to actually parse the C files and generate this information. It should be fairly easy to get this information with clang (this tutorial) might help), you could then make this program write to ~/.vim/after/syntax/c.vim.

  • Thank you for your reply. The clang parser looks cool, but I already created my solution using regex on buffer enter, which is less 'correct' but seems easier to do. This is how I did it if you're interested.
    – R.L
    Commented Aug 8, 2016 at 2:30
  • @R.L You can add that as an answer so that people who find this question in the future (by a search or browser) can be helped by it ;-) Commented Aug 8, 2016 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.