how to highlight variables of c/c++ files in vim

2019-07-15 10:58发布

问题:

I want to highlight variables in a C/C++ file . such as:

    int num;// Highlighted  num
    char str;// Highlighted str

    struct data
    {
     int year;
     int month;
     };
     struct data *p,time;// Highlighted  p  time
     ..........

How to highlight variables as given above (num ,str, p,time.....)

I know we can modify syntax\c.vim to reach that,but how to write the syntax?

Is there any other solution?

回答1:

In general, you need to do two things:

  1. Generate tags using ctags;
  2. Generate syntax file using resulting tags file.

Now some details.

There are plugins to help you. Firstly, plugin Indexer automatically generates tags for a whole project and keeps tags up-to-date. (i'm author of this plugin, so, if you have any problems using it, feel free to ask me)

And secondly, there's plugin TagHighlight to extra highlights variables, enums, typedefs, etc.



回答2:

If you mean how to turn on syntax highligting, then you need to:

:syntax on

If it does not work, determine filetype first:

:set filetype?

Should print "cpp". If not, set it first:

:set filetype=cpp