Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full width background color highlighting of codeblock in Vim

I have Vim documents that have codeblock regions that use 'syntax include' regions to have different syntax highlighting from the main document. For example, I have a syntax region named 'pyregion' defined that I use when I input Python code. The pyregion areas of the document are then highlighted using setup in Python's syntax file, while the area outside the region uses the syntax file of the main document.

I want to have the entire background in the Python region be shaded a different color to distinguish it from the non-Python areas of the document. I can get part of the way there by entering a background for the entire python region:

:highlight pyregion guibg=#555555

But the command above changes the background only for areas that have text characters on them, not for entire background of the region, which seems still to be governed like rest of document by the background color defined for the 'Normal' group. This is okay -- it does draw attention to the region, but it has a blotchy look because only the code characters themselves have different background, not the entire band of the region across the screen.

Is there some way to get a uniform band of a different background color across the whole region, not just portion of region where there are characters?

There's related question and answer in following link, but so far as I can tell that answer also changes background only where there is text: Highlight Code Block Backgrounds with Vim

Thanks for any help.

like image 483
Herbert Sitz Avatar asked Sep 15 '10 21:09

Herbert Sitz


People also ask

How do I change the highlight color in vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.

How do I enable syntax highlighting in vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Why are some words highlighted in vim?

Most filetypes (like python) in Vim come with a syntax that defines highlight groups (see them via :highlight ). A colorscheme then provides combinations of foreground / background color and/or formatting like bold and italic, for terminals, color terminals, and/or GVIM.


1 Answers

I don't believe that this is possible without a ridiculous bodge (see below). If you try this:

:match Error /\%3l/

It highlights what Vim considers to be the whole of line 3. The highlighting only extends to the the end of the characters. This makes me suspect that there will be no way to highlight non-existent characters.


Stop reading now.

Purely as an exercise in the ridiculous, you could of course append a massive number of spaces to the end of every line and then remove them when you save. This would be daft, but it's the only thing I can think of.

" Add spaces to the end of every line (you could also do
" this selectively for lines that don't already have lots
" of spaces and you could do it on reading a file or whatever)
:exe '%s/$/' . repeat(' ', 1000) . '/'
" Automatically remove spaces at the end of every line when saving
:autocmd BufWritePre %s/ *$//
" Make 'A' append at the end of the non-space characters on the line
:nmap A / *$<CR>i
" Make '$' go to the end of the non-space characters on the line
:nmap $ /.\? *$<CR>

Disclaimer: don't do this.

like image 85
DrAl Avatar answered Oct 07 '22 16:10

DrAl