简体   繁体   中英

How to selectively apply syntax highlight

I'm writing a tutorial/book with Vim, and I'd like to turn off the syntax highlighting for a block of text (the normal text of the book) and reactivate it for the code examples.

I have Googled to no end, but I cannot find a simple solution. Am I missing something? Or is this not achievable in Vim?

You can create a syntax file for your book.

For example, you can create a script: ~/.vim/syntax/tutor.vim

"
" tutor.vim -- syntax file for my book/tutor
"
syn include @PY $VIMRUNTIME/syntax/python.vim
syn region pyBlock start="{{{" end="}}}" contains=@PY

This is a sample file:

# File: intro.txt
# Date: 2012-08-19

blah, blah ...
So, I will show you some code:
{{{
    def hello():
        print "world"
}}}

# vim: set syn=tutor :

I have written the SyntaxRange plugin for that (my main use case is highlighting patches inside emails as with the "diff" syntax). With it, you can :[range]SyntaxIgnore or :[range]SyntaxInclude {filetype} certain sections of a buffer, or, when the sections start and end with certain markers, define dynamic sections that adapt when the number of lines change.

Thinking tangentially...

How about using something like restructured text or markdown within Vim and render on github. This gives you version management for free. You can have code-blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM