views:

428

answers:

2

Hi, I use ViM's :highlight CursorLine to change bg color on the current line. But sometimes is the text not readable. I would like a highlight that could only change bg color for the whole line except the text (counting the spaces/tabs in between chars as text). Is it doable? If yes, how? Thanks ;)

+2  A: 

As for as I know, there might be no direct support of setting how the cursorline is highlighted.

But, I've got a trick for doing what you want. That is, after we highlight the cursorline, we can change the color settings of heading/trailing spaces in a line as current "background" and "foreground".

:match NoHighLight /^\s\+|\s\+$/ 
:highlight NoHighLight guibg=background guifg=foreground

A obvious drawback is the part from "the end of the line" to "the boundary of the vim window" will still be painted as the color of cursorline's setting. If it is ugly for you, you can just change the highlight setting of cursorline by only setting its guifg, like:

:highlight CursorLine guifg=red guibg=background 

Maybe there are other neat solutions existed, but that is what I can come up with now. :)

Hope that helps a bit.

Zhaojun
A: 

thx Zhaojun, but it's not what I wanted (also /^\s\+|\s\+$/ doesn't do much, maybe it should be /^\s\+\|\s\+$/) the solution I found is (just example color for elflord color scheme)

:highlight CursorLine gui=none guibg=grey10
:set CursorLine

:highlight NoHighLight guibg=background 
:match NoHighLight /\S\+\(\s\+\|$\)/

it's however not working well for trailing spaces at he end of line, but I usually delete them

to make them visible I use the following

:highlight EndSpaces guibg=green
:match EndSpaces /\s\+$/
pingi
/^\s\+|\s\+$/ means it matches both the leading spaces and the trailing spaces. the "|" is "OR", so it can _NOT_ be escaped as "\|".I've done some basic tests before I posted my solution and it matches both the leading spaces and trailing spaces well. So, I don't know why it doesn't work for you. Maybe, you can just paste some of your text here, so I can figure out why.
Zhaojun
'|' is imho not OR in vim's regex dialect (:help regex, /|, /\\|)some example code:"import os\nfor e in os.environ.keys():\n\tprint e"
pingi
sorry for making the wrong statement about the "|". for your question, a more simple solution might be only set the "guifg" to some different color and make the "guibg" untouched. like::highlight CursorLine guifg=red guibg=backgroundIn this solution, all the spaces will be ignored since they will displayed as the background color, so we don't need to match those spaces at all.
Zhaojun