Tip 1514 Printable Monobook Previous Next

created 2007 · complexity basic · version 6.0

Many programs store configuration information in text files with .ini extension ("dosini" file type). An example of a section from an ini file is:


Vim includes file syntax/dosini.vim for syntax highlighting. This tip shows two enhancements: Folding sections, and correct highlighting when heredoc-style parameters are used.

Folding sections[edit | edit source]

You can enable folding of dosini sections by placing a script in file ~/.vim/after/syntax/dosini.vim on Unix-based systems, or in file $HOME/vimfiles/after/syntax/dosini.vim on Windows systems.

The script should contain:

syn region dosiniSection start="^\[" end="\(\n\+\[\)\@=" contains=dosiniLabel,dosiniHeader,dosiniComment keepend fold
setlocal foldmethod=syntax
" Following opens all folds (remove line to start with folds closed).
setlocal foldlevel=20

Heredoc highlighting[edit | edit source]

Some ini parsers suppport heredoc-style parameter settings, for example:

DefaultText = <<EOT

To correctly handle syntax highlighting with heredoc parameters, create file ~/.vim/after/syntax/dosini.vim (Unix), or $HOME/vimfiles/after/syntax/dosini.vim (Windows), containing lines:

syn match  dosinicomment        "^;.*$\|^#.*$"
syn match  dosinilabel          "^\s\+ ="

" Handle heredoc syntax in the dosini syntax.
" Taken from syntax/perl.ini (:%s/perl/dosini/g).
if exists("dosini_string_as_statement")
  HiLink dosiniStringStartEnd   dosiniStatement
  HiLink dosiniStringStartEnd   dosiniString

HiLink dosiniHereDoc dosiniString

syn region dosiniHereDoc matchgroup=dosiniStringStartEnd start=+<<\z(\I\i*\)+      end=+^\z1$+ contains=@dosiniInterpDQ
syn region dosiniHereDoc matchgroup=dosiniStringStartEnd start=+<<\s*"\z(.\{-}\)"+ end=+^\z1$+ contains=@dosiniInterpDQ
syn region dosiniHereDoc matchgroup=dosiniStringStartEnd start=+<<\s*'\z(.\{-}\)'+ end=+^\z1$+ contains=@dosiniInterpSQ
syn region dosiniHereDoc matchgroup=dosiniStringStartEnd start=+<<\s*""+           end=+^$+    contains=@dosiniInterpDQ,dosiniNotEmptyLine
syn region dosiniHereDoc matchgroup=dosiniStringStartEnd start=+<<\s*''+           end=+^$+    contains=@dosiniInterpSQ,dosiniNotEmptyLine

References[edit | edit source]

Comments[edit | edit source]

Community content is available under CC-BY-SA unless otherwise noted.