Search code examples
vimfile-type

Adding a custom file type in Vim


I want to create a custom ft which incorporates the c syntax highlighting and a highlighting depending on the characters at the start of the line .

Suppose if I call the new extension .mib . I can add:

syntax on
filetype on
au BufNewFile,BufRead *.mib set filetype=c*

to display c syntax highlighting for the .mib files.

If in the same source file there co-exists another set of codes in some other language which are pre-pended by a @@.

How to distinguish [maybe in a different color other than that used for c highlights] these lines keeping the c code highlights for the rest of the code, if the prepended lines are a part of an undefined ft?

How to include the dual syntax highlights, if the @@ prepended lines are a part of an already defined ft?

I am also aware of

:setfiletype html.c

for dual highlighting but I am particularly interested in highlighting the prepended lines.


Solution

  • If your own autocmd is working well for you, you could just use another one to define a new syntax match for your new extension:

    au BufNewFile,BufRead *.mib syn match Error "@@.*"
    

    This will highlight from each occurrence of @@ until the end of that line, using the Error highlighting defined in the c.vim syntax file.

    You can use any existing highlighting group you want, or define your own:

    au ColorScheme * hi mibHighlight guibg=red gui=italic 
    au BufNewFile,BufRead *.mib hi syn match mibHighlight "@@.*"