I'm playing with Vim highlight, so I made the following (syntax.vim):

hi Comment ctermfg=DarkGray
hi LineNR ctermfg=DarkGray
hi CursorLineNR ctermfg=Red
hi String ctermfg=Gray
hi PreProc ctermfg=Brown

syn match MyOp /+/
syn match MyOp /-/
syn match MyOp /*/  containedin=comment contained
syn match MyOp /\// containedin=comment contained
syn match MyOp /&/
syn match MyOp /:/
syn match MyOp /\./
syn match MyOp /=/
hi MyOp ctermfg=LightBlue

syn match MyBrakets /</
syn match MyBrakets />/
syn match MyBrakets /(/
syn match MyBrakets /)/
syn match MyBrakets /{/
syn match MyBrakets /}/
syn match MyBrakets /\[/
syn match MyBrakets /\]/
hi MyBrakets ctermfg=Yellow

syn keyword MyNSutl utl 
hi MyNSutl ctermfg=Gray

syn keyword MyNSmmi mmi
hi MyNSmmi ctermfg=DarkBlue

syn match MySend /send_\w*/
hi MySend ctermfg=Red

syn match MyGet /get_\w*/
hi MyGet ctermfg=Magenta

syn match MyPrint /print_\w*/
hi MyPrint ctermfg=Gray

syn match MyMPI /MPI_\w*/
hi MyMPI ctermfg=LightGray

I was testing it with a single file, while in Vim with my .cpp file open I did so syntax.vim and the magic happened. So far so good.

Next step was to use it in the session I was working, Vim -S session.vim to open some files in split view, and so syntax.vim. Problem. Syntax applies to just one file, current split. So I have to repeat that to all files - not good.

After some googling I found that correct way to proceed was to rename it to i.e. cpp_my.vim and put in ~/.vim/ftplugin/myplugin/. Did that, and it workds partially, some settings don't get applied. Then I went to Vim doc section 41.11 Writing a plugin. Things are getting complicated and here I'm.

The hack I did isn't general purpose, its just for the project I'm working these days. So making it a plugin don't smell good to me.

So in order to use it, the best approach is to manually so syntax.vim to each file? Or is there some clever alternative?

up vote 1 down vote accepted

The correct way is to drop the file in {rtp}/syntax/{ft}.vim. Given the proximity between C and C++, I'd say in $HOME/.vim/after/syntax/c.vim -- in after/ to be sure to source the standard configuration before the improvements.

PS: IIRC my experiments, the less syn match you have, the fastest highlighting will be. match is made to support regexes, you can then merge several of your single character patterns into a single pattern => "[+=^&~<>:-]"

  • It works if I put it on{rtp}/syntax/cpp not c. – KcFnMi Aug 10 at 3:35
  • Indeed. My mistake. It's because somewhere in my {rtp}/syntax/cpp.vim and c.vim I execute :runtime! syntax/c-*.vim syntax/c_*.vim. I've edited my answer. – Luc Hermitte Aug 10 at 9:03

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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