2

I can do this for markdown's tab-based code blocks with mkdCode color group. So my question is if it's possible to do the same for fenced once?

I use plasticboy/vim-markdown plugin.

Here's a minimal vim config to reproduce the behaviour ~/.config/nvim/test-init.vim:

filetype plugin indent on
call plug#begin()
Plug 'plasticboy/vim-markdown', {'for': 'markdown'}
call plug#end()
highlight mkdCode ctermbg=yellow

Here's the result I get with it:

enter image description here

It seems that after applying mkdCode background it rewrites by code highlighting.

  • Looks like you're using the vim-markdown plug-in. Yes, it should highlighting fenced code blocks with mkdCode too, see this rule which matches <code>```</code> on its own line to match start and end of the block... Is that not working for you? Can you show an example of it not working? – filbranden Jun 7 at 14:12
  • 1
    @filbranden right, sorry I forgot to mention about the plugin, I've updated the question – imbolc Jun 7 at 14:58
2

I use a technique I picked up on reddit from /u/fob3sg:

function! ColorCodeBlocks() abort " {{{1
  setlocal signcolumn=no

  sign define codeblock linehl=codeBlockBackground

  augroup code_block_background
    autocmd! * <buffer>
    autocmd InsertLeave  <buffer> call s:place_signs()
    autocmd BufEnter     <buffer> call s:place_signs()
    autocmd BufWritePost <buffer> call s:place_signs()
  augroup END
endfunction

This creates a function that (mis)uses the signcolumn feature to change the background color of code blocks. To use it, add something like this e.g. in your after/ftplugin/markdown.vim:

highlight codeBlockBackground ctermbg=yellow
call ColorCodeBlocks()

I find it actually works very well. Obviously, you can change the color to something that fits your colorscheme.

| improve this answer | |
2

What you're seeing here is vim-markdown's support for highlighting embedded languages in fenced code blocks.

In short, vim-markdown will recognize the python in ```python and it will use Python syntax highlighting for that code block.

You can find the code that implements that here.

It will create a new syntax highlighting group mkdSnippetPYTHON and have that block include Python syntax rules.

In theory, you could change the background of that specific group:

highlight mkdSnippetPYTHON ctermbg=yellow

But I found that doesn't really work great, since Python keywords (such as if, while, def, etc.) and other highlighted terms (such as function names in a function definition) will have been styled by Python and will override your background setting, showing a normal background.

Note that this doesn't happen if you don't have a language in your fenced block, if you start it with only ``` or if you have it for a language that's invalid or not recognized. (If you really wanted to keep the python language tag but not use syntax highlighting here, perhaps mapping 'python' to an invalid value in g:vim_markdown_fenced_languages would be a somewhat hacky way to disable this behavior.)

| improve this answer | |
  • 1
    Yeah, I've just played a bit with it too and couldn't find a simple solution to make the background consistent. But now I understand you it works, thanks :) – imbolc Jun 8 at 2:50

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.