sponsor Vim development Vim logo Vim Book Ad

SyntaxRange : Define a different filetype syntax on regions of a buffer.

 script karma  Rating 27/9, Downloaded by 700    Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
This plugin provides commands and functions to set up regions in the current
buffer that either use a syntax different from the buffer's 'filetype', or
completely ignore the syntax.

- If you also want different buffer options (like indent settings, etc.) for
  each syntax region, the OnSyntaxChange.vim plugin (vimscript #4085) allows
  you to dynamically change the buffer options as you move through the buffer.

- If the highlighting doesn't work properly, you could alternatively edit the
  range(s) in a separate scratch buffer. Plugins like NrrwRgn (vimscript #3075)
  provide commands to set these up, with automatic syncing back to the
  original buffer.

The code to include a different syntax in a region is based on

For quick, ad-hoc manipulation of the syntax withing a range of lines, the
following commands are provided:

:[range]SyntaxIgnore    Ignore the buffer's filetype syntax for the current
                        line / lines in [range]. (Top-level keywords will
                        still be highlighted.)
                        This can be a useful fix when some text fragments
                        confuse the syntax highlighting. (For example, when
                        buffer syntax set to an inlined here-document is
                        negatively affected by the foreign code surrounding
                        the here-document.)

:[range]SyntaxInclude {filetype}
                        Use the {filetype} syntax for the current line / lines
                        in [range].

                        Line numbers in [range] are fixed; i.e. they do not
                        adapt to inserted / deleted lines. But when in a
                        range, the last line ($) is interpreted as "end of

For finer control and use in custom mappings or syntax tweaks, the following
functions can be used. You'll find the details directly in the
.vim/autoload/SyntaxRange.vim implementation file.

SyntaxRange#Include( startPattern, endPattern, filetype, ... )
                        Use the {filetype} syntax for the region defined by
                        {startPattern} and {endPattern}.
SyntaxRange#IncludeEx( regionDefinition, filetype )
                        Use the {filetype} syntax for the region defined by

To highlight the text between the markers
    int i = 42;
with C syntax, and make the markers themselves fade into the background:
    :call SyntaxRange#Include('@begin=c@', '@end=c@', 'c', 'NonText')

To highlight inline patches inside emails:
    :call SyntaxRange#IncludeEx('start="^changeset\|^Index: \|^diff \|^--- .*\%( ----\)\@<!$" skip="^[-+@       ]" end="^$"', 'diff')
To install this automatically for the "mail" filetype, put above line into a
script in ~/.vim/after/syntax/mail/SyntaxInclude.vim
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim SyntaxRange*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.022 or

To automatically include a syntax in a certain {filetype}, you can put the
command into a script in
If you want to include a syntax in several (or even all) syntaxes, you can put
this into your vimrc:
    :autocmd Syntax * call SyntaxRange#Include(...)

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
SyntaxRange-1.02.vmb.gz 1.02 2015-04-23 7.0 Ingo Karkat - Set main_syntax to the buffer's syntax during :syntax include of the subordinate syntax script. Some scripts may make special arrangements when included. Suggested by OOO.
- Handle :.SyntaxInclude and :.SyntaxIgnore on folded lines correctly. Use ingo#range#NetStart/End().
- Add dependency to ingo-library (vimscript #4433). *** You need to separately install ingo-library (vimscript #4433) version 1.022 (or higher)! ***
SyntaxRange-1.01.vmb.gz 1.01 2013-11-21 7.0 Ingo Karkat Avoid "E108: No such variable: b:current_syntax" when the (misbehaving) included syntax doesn't set it. Reported by o2genum at http://stackoverflow.com/a/16162412/813602.
SyntaxRange.vba.gz 1.00 2012-08-13 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
SourceForge.net Logo