Highlight syntax inside Markdown

4.8
(34)

Modern versions of Vim include support for syntax highlighting and filetype handling of Markdown files. Tim Pope maintains this functionality in his vim-markdown repo but this isn’t a plugin that needs to be installed. Vim includes this syntax file in its source distribution. So this is functionality you already have, and functionality that is essential if you’re using Vim to take notes in Markdown format.

But you may not realize that Vim is able to highlight the syntax of fenced code blocks. Fenced code blocks are code snippets you embed in Markdown with triple backticks. Markdown supports a language identifier after the backticks which will indicate to editors and renderers how to highlight the syntax inside the block. For example:

```ruby
def full_name
  "#{first_name} #{last_name}"
end
```

Vim can properly highlight this syntax, but this functionality is not enabled by default. To enable it, you need to specify which languages you wish to support. Put something like this in your .vimrc:

let g:markdown_fenced_languages = ['html', 'python', 'ruby', 'vim']

But feel free to specify any and all languages you regularly embed in Markdown documents. In the demo below, you can see various languages I have listed in my config. When I add vim to the start of the fenced block, Vim instantly highlights the code as VimL.

How useful was this tip?

Average rating 4.8 / 5. Vote count: 34

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

92 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

3 thoughts on “Highlight syntax inside Markdown”

Leave a reply

Your email address will not be published. Required fields are marked *