Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing two syntax highlighting scripts

Tags:

vim

I write blog posts with Jekyll, and for that I end up using three different languages in the same file: YAML at the start for post metadata, Markdown in the body, and C++ in code snippets.

I tried to setup a script so that I can have vim highlight all three properly for me, and ended up with something like this in syntax/jekyll.vim:

" Build upon Markdown syntax
runtime! syntax/markdown.vim

" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in YAML syntax for front matter
syntax include @Yaml syntax/yaml.vim
syntax region yamlFrontmatter start=/\%^---$/ end=/^---$/ keepend contains=@Yaml

" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in C++11 syntax for code snippets
syntax include @Cpp syntax/cpp.vim
syntax region cppCodeSnippet matchgroup=Comment start=/^{% highlight cpp %}$/ end=/^{% endhighlight %}$/ keepend contains=@Cpp

let b:current_syntax='jekyll'

I also set up a file detection script to set ft to this syntax.

It almost works. When I open a file that gets detected as this type, I get everything correct except for the C++ highlights. However, if I type :syn on after that, everything works fine. I can delete the buffer and open the file again and all highlights are ok. If I close vim and start it again, I need to run :syn on again.

What am I missing? How can I debug this issue?

like image 318
R. Martinho Fernandes Avatar asked Apr 24 '13 20:04

R. Martinho Fernandes


1 Answers

Quick fix: append syntax on to the last line of your .vimrc, which is the same as setting :syn on in the live session.

Not So Quick:

Looks like you might have installed the custom 'jekyll' syntax alongside the default syntax files in $VIMRUNTIME.

According to Vim wiki section on custom syntax, it's preferable to keep all personal customizations within ~/.vim. For example, putting your jekyll.vim syntax in ~/.vim/syntax/.

Do not use a directory containing the files distributed with Vim because that will be overwritten during an upgrade (in particular, do not use the $VIMRUNTIME directory).

In the Vim syntax docs:
:syntax enable runs ':source $VIMRUNTIME/syntax/DEFAULT_SYNTAX.vim'.
:syn on (or :syntax on) will "overrule your settings with the defaults".

So if setting :syntax on makes your custom syntax work, it must be contained in the default syntax set.

Try keeping all the custom stuff in ~/.vim and see if that settles things.

like image 68
shender Avatar answered Dec 30 '22 11:12

shender