12

I am often writing documents in knitr using vim, which means that I have markdown code mixed with R code. I would like to have R syntax coloring on R chunks, but markdown or no coloring on all the other fragments. Is this doable? How?

  • 3
    I think you can, try looking at vim.wikia.com/wiki/… – nobe4 Mar 8 '16 at 11:43
  • 5
    Depending on your markdown plugin, you may or may not have already support for syntax coloring of included "code blocks". In the first case, you will (just) have to alter the plugin's syntax to recognize your "R chunks" as valid code blocks. In the 2nd case, perhaps you can inspire yourself from Junegunn's vimrc, especially the functions s:syntax_include and s:file_type_handler: github.com/junegunn/dotfiles/blob/master/vimrc – VanLaser Mar 8 '16 at 12:02
  • 5
    Is this a markdown file that happens to have R code inside of code blocks? If so you may want to add R to your g:markdown_fenced_languages in your vimrc e.g. let g:markdown_fenced_languages = ['r']`. I am assuming you are using the default markdown syntax provided by Tim Pope. More on fenced languages – Peter Rincker Mar 8 '16 at 18:15
  • 1
    Vim syntax files are, like most things in Vim, just a sequence of Ex commands put in a file. You can include one, two, three, or as many as you want. Creating a syntax file which highlights some blocks as language A and other blocks as language B is not that hard (actually, I just did this in another answer). However, I'm not entirely sure whatthe "knitr" documents look like? Is this just Markdown? Or a different syntax? – Martin Tournoij Mar 9 '16 at 4:41
2

As Peter Rincker notes, Vim's standard markdown formatting already includes support for highlighting r code within a "fenced code block", and to enable it, you just have to set a global variable with code in your vimrc:

let g:markdown_fenced_languages = ['r']

However, after doing so, you will find that your knitr code blocks are still not highlighted.

This is because knitr uses Pandoc's formatting of info strings in order to differentiate R code that is to be run from a regular code block, and Vim's regular Markdown highlighting doesn't support this1.

The easiest way to fix this is to manually add an extra syntax item to match this type of code block by adding the following line to ~/.vim/after/syntax/markdown.vim

syntax region markdownHighlightr matchgroup=markdownCodeDelimiter start=/^\s*```{r}.*$/ end=/^\s*```\ze\s*$/ keepend contains=@markdownHighlightr

You could also consider installing a Vim plugin for Pandoc highlighting, which would presumably support this out of the box.

1: This can't really be considered an issue in Vim's markdown syntax file. The original Markdown syntax doesn't include fenced code blocks at all, and the CommonMark spec doesn't define how info strings should be formatted.

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.