devnull
devnull

Reputation: 2862

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?

Upvotes: 1

Views: 289

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172510

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.

Upvotes: 3

kev
kev

Reputation: 161604

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 :

Upvotes: 3

bdutta74
bdutta74

Reputation: 2860

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.

Upvotes: 2

Related Questions