Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to syntax-highlight a part of file in a different syntax?

Tags:

vim

I installed bnf.vim (highlights BNF grammar files).

Suppose I have a comment in my code:

/* <BNF>

<S> := <A> | h
<A> := a | b | c | .

</BNF> */

Can Vim be somehow programmed to highlight that comment in BNF syntax, despite the filetype of the whole file?

like image 932
Andrey Moiseev Avatar asked Apr 23 '13 06:04

Andrey Moiseev


Video Answer


2 Answers

You can use my SyntaxRange plugin for that.

Either explicitly assign the range, e.g.

:10,20SyntaxInclude bnf

or automatically based on the delimiting patterns:

:call SyntaxRange#Include('<BNF>', '</BNF>', 'bnf')
like image 147
Ingo Karkat Avatar answered Oct 18 '22 02:10

Ingo Karkat


for many file types vim will use the heredoc names as a clue. For example in php files I'm often creating here docs thus:

<?php 

$my_html = <<<html 
<h1>Solar</h1> 
<p>Is <em>good</em></p> 
html; 

$my_js = <<<javascript 
alert('yeah baby'); 
javascript; 

$my_sql = <<<sql 
SELECT user 
FROM mytable 
WHERE energy = 'solar'; 
sql; 

works beautifully, with only problem being that indentation is problematic (the closing marker cannot have preceding spaces)

like image 1
ErichBSchulz Avatar answered Oct 18 '22 04:10

ErichBSchulz