Is there an analog of mmm mode for vim? This would allow me to have syntax highlighting of several modes in the same file.

  • 2
    BTW: in the vim world very few will probably know emacs terminology. You should add a short explanation what mmm is. Additionally to the reference to mmm, you could have used the words nested or fenced code. I have raised a related question with a more general scope not limited to syntax highlighting. However, I agree it is too far fetched for a general solution. – Hotschke Jan 2 at 19:29

This is typically done using :syn-include to include another language's syntax rules for a specified region of a parent syntax file.

The child syntax needs to be included as its own cluster:

syn include @otherLang path/to/syntax/otherLang.vim

Then a region in the parent syntax is defined where the child syntax should apply:

syn region otherLangRegion start=startRegex end=endRegex contains=@otherLang

You can see this pattern in the syntax file for vim script itself (which includes highlighting for lua, perl, ruby, python, tcl, and mzscheme) as well as the html syntax file (which allows highlighting javascript, css, and vb).

  • Thank you so much! This looks easy! I want to use Latex as the parent syntax, and python as the minor syntax in my .tex files. Should I add these commands to .vimrc or to latex.vim? – smilingbuddha Jan 1 at 16:19
  • ~/.vim/after/syntax/tex.vim would probably be the best place to do it. That allows you to apply your changes on top of the standard syntax/tex.vim. – jamessan Jan 2 at 17:19
  • @smilingbuddha: the plugin vimtex contains the file after/syntax/tex.vim which already does this for python, gnuplot, asymptote, dot language (graphviz), lua and minted environments. If you miss something, the author of vimtex will very likely help you and improve vimtex. – Hotschke Jan 2 at 19:19

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.