I am looking for a possible solution for applying two different background colors inside a single vim buffer depending on context like in this sublime text example.One use case of that is to color code snippets inside markup files differently so they stand out more.However I have never seen an example with a setup like that.Is this at all possible in vim?
Is it possible to use two different color backgrounds in a single vim buffer?
colorscheme
Because I was curious how well this would work, I've hacked something togetherthat sort of works.As mentioned in the comments on the earlier answer, the only way to do this isto fill up regions with spaces; which is exactly what we do; before write, weremove these spaces, so you shouldn't be bothering anyone else with it.Note that this example is very specific for the markdown filetype!Side effects:Copying text will also copy a lot of spacesUsing $ and End no longer work as expected (it goes to column 80), and keys such asj and k also behave different.The background is not displayed when 'list' is enabledCompletely blank lines don't work, you need to manually add a tab or 4 spaces... Perhaps more?First, you need to add this to your ~/.vim/after/syntax/markdown.vim:syn clear markdownCodeBlock                                                 syn region markdownCodeBlock start=    \|\t end=$                       hi def markdownCodeBlock ctermbg=230 guibg=lightyellow                      You can adjust the colours to your liking, of course ;-)Then, add this to your vimrc:fun! MarkdownBlocks()                                                           fun! s:fill(line)                                                                Remove all trailing whitespace                                            let l:line = substitute(a:line,  *$, , )                               Add trailing whitespace up to 'textwidth' length                          return l:line . repeat(' ', (&tw > 0 ? &tw : 80) - strdisplaywidth(l:line))    endfun                                                                       Get all lines in a list                                                   let l:lines = getline(1, line('$'))                                          Map s:fill() to the lines that are a code block                           call map(l:lines, 'v:val[0] == \t || v:val[:3] ==      ? s:fill(v:val) : v:val')     Reset the buffer to the lines                                             call setline(1, l:lines)                                                endfun                                                                       Remove all the trailing spaces                                            fun! MarkdownBlocksClean()                                                      let l:save_cursor = getpos(.)                                             silent %s/^\(    \|\t\)\(.\{-}\)\( *\)$/\1\2/e                              call setpos('.', l:save_cursor)                                         endfun                                                                      au BufWritePre *.markdown call MarkdownBlocksClean()                         Set spaces on loading the file, leaving insert mode, and after writing it au FileType markdown call MarkdownBlocks()                                  au InsertLeave *.markdown call MarkdownBlocks()                             au BufWritePost *.markdown call MarkdownBlocks()                            I'm not going to explain the code line-by-line, the comments should makethe general gist of it clear ;-)