1

Our software has many .xml configuration files. Vim colors these files, using e.g. /usr/share/vim/vim74/syntax/xml.vim. But can we customize the coloring for just these files, and not for all .xml files?

(An example of such customizing would be coloring the text between <schmoo> and </schmoo> tags. But this question isn't about how to color, nor is it about XML. I hope for an answer that applies equally well to Python or YAML or hundreds of other formats.)

Can another file in the .xml files' directory override or extend syntax/xml.vim? (Unlikely: runtimepath doesn't mention the current directory; neither do autochdir or getcwd() seem to help.)

What about something in an environment variable, or in an .xml file itself (at the top or bottom; or inline like jsbegin/jsend in Vim custom syntax highlighting, include other language syntax in a specified range)?

Or must the extensions go into each user's ~/.vim/after/syntax/xml.vim (which is used for all .xml files)? That would be suboptimal, but tolerable for extensions specialized enough to not collide with other things (like "schmoo" above).

Related: How can I configure syntax highlighting for a custom language in vim?

Reference: http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-files

2

Ensure that vim's modeline is on. :set modeline? should respond modeline instead of nomodeline. If necessary, set it in your vimrc. (Security concerns are pretty much obsolete by now.)

In a particular .xml file, include this line: // vim: filetype=xmlWithSchmoos (If not xml, replace // with whatever starts a comment in that language.)

Create ~/.vim/syntax/xmlWithSchmoos.vim or /usr/share/vim/vim74/syntax/xmlWithSchmoos.vim, containing the single line :so $VIMRUNTIME/syntax/xml.vim.

Append stanzas to xmlWithSchmoos.vim. For example, for red schmoo tags and what's between them, append:

:syntax region xyzzy start=/<schmoo>/ end=/<\/schmoo>/
:highlight xyzzy ctermfg=red guifg=red

Of course, rename to something meaningful the placeholders schmoo, xml, xmlWithSchmoos, and xyzzy.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.