سؤال

Syntax highlighting works swimmingly beautiful with the standard types, like int, uint32_t, float and so on. However, I would like to teach vim that there are other types defined with typedef in my code, e.g.

typedef double float64_t;

How can I make vim use the same highlighting for float64_t as for the standard types? A solution with a local file (e.g. within my ~/.vimrc or .vim directory) would be preferred. Automatic parsing of typedef names not a requirement, I'm willing to add typedef names as needed.

هل كانت مفيدة؟

المحلول

Here is a way to add the names as needed.

For Windows, create (replace vimfiles as appropriate)

~\vimfiles\after\syntax\c.vim

and add lines defining new syntax highlighting items. For example (from my cpp.vim),

" add nullptr as a keyword for highlighting
syn keyword Constant nullptr

To determine which group you want to add to, open a c file and type :syntax and you can look through the existing syntax groups.

نصائح أخرى

I also found out that we can use the match command to cover a set of typedef names described by a pattern:

match Type /\w*_t/

will highlight as a type all typedef names ending in _t (but will do so everywhere, even inside comments and string literals.)

You can match only those identifiers ending with _t (not those that contain _t partway through like foo_table) by using negative lookahead.

Place in ~/.vim/after/syntax/c.vim:

syn match cType "\h\w*_t\w\@!"

\h (head of word) means the same as [A-Za-z_] i.e. it will match any character that can start an identifier in C. \w* (word) means the same as [0-9A-Za-z_]* i.e. it will match any characters that can continue an identifier in C. _t will literally match those two characters. \w\@! is negative lookahead: it will match as long as \w does not match i.e. it will match as long as the _t is not followed by an alphanumeric character or an underscore.

Note: This is subtly different to \h\w*_t\W which will match a foobar_t type followed by something other than [0-9A-Za-z_], which means it won't match at the end of the line or end of the file. That pattern will also match and highlight as a keyword that following character, which is definitely not what you want.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top