Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vimscript - Programmatically get colors from colorscheme

Tags:

vim

editor

I'm trying to create an autocmd in Vimscript which sets a few attributes of the colorscheme as I want them. Specifically, I'm trying to make the NonText background have the same color as the regular background.

The problem is, I have no idea how to get a colorscheme's background color from within vimscript.

Anyone have any ideas?

Edit:

So here's what I'm trying to achieve. Actually a few things:

  1. Originally, I was trying to hide the "~" that's in front of all non-existant lines. Someone suggested setting it as the same color as the background, so I added an autocmd that did this: hi NonText guifg=bg.

  2. After doing that, I realized that some colorschems have a different background color for the regular lines and the "non-existant" part of the buffer. This is a problem, since my autocmd sets the NonText color to be the same as the regular background, not the special "non-existant" background.

    Also, I decided that even without the whole "get rid of the ~" issue, I don't like it when the non-existant parts of buffers are a different color.

Lastly, I'm trying to learn about vim colorschemes in general, since I'm planning to write a plugin that will need some other tricks like figuring out the colors from schemes.

like image 646
Edan Maor Avatar asked Mar 28 '12 16:03

Edan Maor


2 Answers

There are two approaches here:

  1. Use hi NonText. Not alone, but with :redir, of course. I won’t describe it more verbose because I personally don’t like any solution using :redir for a number of reasons (they are: 1. requires parsing 2. no nested redirections 3. no way to know whether redirection is active).
  2. Use synIDattr() and hlID():

    let bgcolor=synIDattr(hlID('NonText'), 'bg#')
    

    will assign something in form "#rrggbb" (just "N" in terminal) to variable bgcolor, or -1 if background was not defined for this group. You can use this to construct :hi command (regular background should be defined in Normal group).

  3. Just use

    hi link NonText Ignore
    

    . Works unless your colorscheme has redefined Ignore group so that it actually is shown.

like image 178
ZyX Avatar answered Sep 23 '22 19:09

ZyX


hi NonText guibg=bg

How about that?


EDIT after clarification from the OP:

Okey, let's go from the start. For now I'd put writing of a plugin on hold for a while until you get acquinted with the basic Vim settings and language. The characters, one of which is the ~ you're trying to hide, are so called list characters, and they can be defined in the listchars option. What yours are you can see by set listchars?. They can also be turned on/off (visible or invisible, I mean) by either set list / set nolist, or toggled on/off with set invlist.

The NonText highlighting group is the one that "covers" the display of those characters, but really the way to turn them on/off is via the setting, not via overwriting the background/foreground color of that group. Therefore my original confusion over your intentions. There is also a highlighting group SpecialKey that you might also find interesting, since it cover some cases.

like image 44
Rook Avatar answered Sep 22 '22 19:09

Rook