Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set alternating highlight colors to text in Vim?

Tags:

linux

vim

vi

Is it possible to set alternating (one color for lines with odd line number and another for even line numbers) highlighting colors for each row in Vim?

like image 700
Maduranga E Avatar asked Jan 10 '23 13:01

Maduranga E


2 Answers

This can do what you want with text background colors:

syn match Oddlines "^.*$" contains=ALL nextgroup=Evenlines skipnl
syn match Evenlines "^.*$" contains=ALL nextgroup=Oddlines skipnl

hi Oddlines ctermbg=yellow guibg=#FFFF99 
hi Evenlines ctermbg=magenta guibg=#FFCCFF

Just add this to .vimrc or the right file type .rc you want.

Since this utilizes the syntax functionality, it only applies to matchable typed text. I don't know if there's a way to alternate the background color of the empty "space" after text that hi Normal ctermgb=darkblue guibg=darkblue does.

like image 178
Kache Avatar answered Jan 18 '23 11:01

Kache


There's nothing built-in, so you would have to emulate that (and suffer from the consequences like slow performance, bad interference, etc.) A candidate would be :match / :call matchadd(), because that is independent of syntax highlighting. Demo:

hi Alternate guibg=LightGrey guifg=NONE
execute 'match Alternate /\%(' . join(map(range(1,100), '"\\%" . v:val * 2 . "l"'), '\|') . '\)/'
like image 30
Ingo Karkat Avatar answered Jan 18 '23 10:01

Ingo Karkat