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

后端 未结 3 619
陌清茗
陌清茗 2021-02-08 02:19

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

Suppose I have a comment in my code:

/* 

 :=  | h
 := a | b |          


        
相关标签:
3条回答
  • 2021-02-08 03:05

    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')
    
    0 讨论(0)
  • 2021-02-08 03:09

    My use case was highlighting an inline Lambda function in a CloudFormation template. Using the above-mentioned SyntaxRange plugin, I added this to my .vimrc:

    autocmd Syntax * call SyntaxRange#Include('vim: set ft=js:','vim: set ft=js:','javascript')
    

    And then in my template:

      Function:
        Type: AWS::Lambda::Function
        Properties:
          FunctionName: myFunction
          Code:
            ZipFile:
              !Sub |
                // vim: set ft=js:
    
                const aws = require('aws-sdk');
                const aws4 = require('aws4');
                const https = require('https');
                const zlib = require('zlib');
    
                ...
    
                // vim: set ft=js:
    
    # vim: set ft=yaml:
    
    0 讨论(0)
  • 2021-02-08 03:14

    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)

    0 讨论(0)
提交回复
热议问题