The default markdown syntax highlighting configuration for Vim and Neovim does not know how to highlight markdown frontmatter that is commonly used in static site generators.
In this article I will show how you can add a syntax region that tells Vim/Neovim to highlight the frontmatter with a different syntax.
The configuration is based on this article, which you should also take a look at, as it will also tell you how you can fix folding.
Both Vim and Neovim have the facility to extend syntax definitions by sourcing config files in the users config folder.
This is done by creating a file in the
~/.config/nvim/after/<syntax>.vim for Neovim. In our case,
is to replaced with
Since I'm using Neovim, and I'm using Zola as a static site generator (Zola uses
+++ as starting and ending block for frontmatter, with a TOML syntax), I need
to write the following to my
unlet b:current_syntax syntax include @Toml syntax/toml.vim syntax region tomlFrontmatter start=/\%^+++$/ end=/^+++$/ keepend contains=@Toml
However, if I were using a different static site generator where the frontmatter
block is defined with
---, and the syntax was YAML, I would write:
unlet b:current_syntax syntax include @Yaml syntax/yaml.vim syntax region yamlFrontmatter start=/\%^---$/ end=/^---$/ keepend contains=@Yaml
This is a combined configuration that should highlight
blocks as TOML and
--- blocks as YAML.
Create the markdown syntax extension configuration (Neovim:
~/.vim/after/markdown.vim) with the
unlet b:current_syntax syntax include @Yaml syntax/yaml.vim syntax include @Toml syntax/toml.vim syntax region yamlFrontmatter start=/\%^---$/ end=/^---$/ keepend contains=@Yaml syntax region tomlFrontmatter start=/\%^+++$/ end=/^+++$/ keepend contains=@Toml