Vim supports syntax highlighting for embedded languages or fenced code in markdown (:h :syn-include).

However, you usually want that other vim settings and mappings are adjusted for embedded languages as well. These are for example

  • load/unload ftplugins containing settings for example for
    • b:match_words used by matchit's %
    • commentstring (e.g. for vim-commentary)
    • square bracket motions (e.g. done in VIMRUNTIME/ftplugin/[vim,ruby,python,...].vim)
  • folds (foldmethod or foldexpr)
  • indentation scripts in $VIMRUNTIME/indent/<language>.vim (indentexpr)
  • formatting (formatprg or dedicated plugins autoformat, neoformat)
  • 'intellisense' completion (omnifunc or dedicated plugins ycm, deoplete, ncm2)
  • linting (run compiler for code region or dedicated plugins ale, neomake or syntastic)
  • ft snippets (ultisnips or snipmate)
  • ...

Plugins I know which are dedicated to this or have builtin support are

It is one thing to adjust a single setting as done by https://github.com/suy/vim-context-commentstring but I miss support for the adjustments done in $VIMRUNTIME/ftplugin and the $VIMRUNTIME/indent/.

Afaik, this is something which vim does not really cover. For example, I use the plugin SimpylFold which defines folds for python as I want it and vimtex for latex files. However, I would not know if vim can combine this for a single latex file using pythontex.

I am not familiar with other editors and their solutions for this. A quick search revealed for Visual Studio Code that vscode supports out of the box adjusted syntax highlighting and commenting.

Did I miss something in vim itself supporting loading/unloading ftplugin/indent or do you occasionally change the filetype by yourself?

Today many tasks (e.g. autocompletion, linting, snippets, formatting) are provided by dedicated plugins: Which of the major plugins i.e. ycm, deoplete, ale, syntastic, tagbar, ultisnips, snipmate, autoformat and neoformat support this?

  • For syntax there is SyntaxRange. If you expect a generic solution that works out of the box with any template and / or embedded languages, you're chasing the pie in the sky. It's a hard problem, that can be solved (given enough effort) for particular languages. Solving it generically is not possible.
    – lcd047
    Oct 28 '18 at 17:51
  • 1
    SyntaxRange was not what I was looking for. vimtex already does this automatically for latex environments which contain different languages (python, lua, gnuplot, graphviz, asymptote, minted-languages). I would expect the same from other ftplugins which can contain embedded languages, e.g. html/css/javascript. But there is another plugin mentioned: OnSyntaxChange. So thanks for pointing me into this direction. I certainly agree that a generic solution is not feasible. I was interested in the experience of others and their take on this.
    – Hotschke
    Oct 29 '18 at 6:32
  • I thought the recommended way to source several files was to use a "combined" filetype, separated by ., e.g. :set ft=php.sql (just a silly example, as I don't know if those actually work) Jun 23 at 12:41

Your Answer

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

Browse other questions tagged or ask your own question.