2

I was writing some filetype-specific vim scripts, c.vim and cpp.vim, when I noticed that for C++ files, both c.vim and cpp.vim were being executed. This makes a lot of sense, since C++ is a superset of C. But my question is this: where is the logic that makes this happen?

That is, is something baked in when vim is compiled that tells it that C++ files are also C files, or is there some file in the .vim directory that controls this behavior? Or something else entirely?

It might be interesting to note that checking the filetype vim has assigned to my C++ files (using :set ft?) returns cpp, not something like c.cpp, as discussed in this question.

Community
  • 1
  • 1
ravron
  • 11,014
  • 2
  • 39
  • 66

2 Answers2

5

My cpp.vim file (under /usr/share/vim/vim73/syntax) has in it:

" Read the C syntax to start with
if version < 600
  so <sfile>:p:h/c.vim
else
  runtime! syntax/c.vim
  unlet b:current_syntax
endif

So it's reading the c.vim file.

lurker
  • 56,987
  • 9
  • 69
  • 103
  • Nice one. I didn't even know about the existence of the `/usr/share/vim73` directory (although, should you care to know, mine is actually at `/usr/share/vim/vim73`). Thanks! – ravron Jun 19 '13 at 23:39
  • @Riley... you found my typo, which I fixed. Mine's in the same location. :) – lurker Jun 19 '13 at 23:44
0

I'm not sure about this, but I'd expect that cpp.vim needs c.vim when using the extern "C" {} construct. It does not mean that C++ is seen as a superset of C from vim's eyes. But I did not read thoroughly the syntax file to be sure of that, because vim syntax files are painful to read ;-)

Otherwise, vim recognizes the file you're editing as a C++ file and loads its syntax through the ft mechanism, that only loads a set of syntax, plugins etc.. given the extension of the file (or the first chars, iinw).

HTH

zmo
  • 24,463
  • 4
  • 54
  • 90