Friday, 23 August 2019

Vim custom syntax highlighting, include other language syntax in a specified range


VIM 7.3.46


I have a custom syntax file defined for making my notes more readable.


I want to define a range that will apply syntax highlighting from an existing syntax file (e.g. php, javascript or whatever) within certain boundary characters.


For example,


Notes.txt
Notes would be here, blah blah...
More notes, then a javascript code block with proper js highlighting below this:

**jsbegin**
$('#jquerystuff').change(function(){
var example = $(this).val();
alert(example);
});
**jsend**

So I'm looking for something like this to put in the vim syntax file:


so :p:h/javascript.vim
so :p:h/php.vim

syn region notesJS matchgroup=javascript start="**jsbegin**" end="**jsend**" contains=javascript
syn region notesPHP matchgroup=php start="**phpbegin**" end="**phpend**" contains=php

But it must only apply javascript highlighting to text within the defined range:



Answer



The required lines are as follows:


" Include PHP highlighting between **phpbegin** and **phpend** tags
syn include @notesPHP syntax/php.vim
syn region phpCustom start=+\*\*phpbegin\*\*+ keepend end=+\*\*phpend\*\*+ contains=@notesPHP

" Include JavaScript highlighting between **jsbegin** and **jsend** tags
syn include @notesJavaScript syntax/javascript.vim
syn region javaScriptCustom start=+\*\*jsbegin\*\*+ keepend end=+\*\*jsend\*\*+me=s-1 contains=@nJavaScript

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...