10

I need this in order to finish a plugin I'm making in order to change the colors of the statusline depending on the mode.

However I've run across an obvious problem, how would I know what theme the user is using? And even if I knew I can't make specific colors for each theme.

So, how to know the foreground and background colors that the current theme is using for the statusline for example.

EDIT 1:

In case I wasn't clear, I'm looking for a way to find out the ctermfg/bg and guifg/bg for certain stuff. Right now the statusline. :)

EDIT 2:

Apparently if I do :hi StatusLine I get what I need, however I need to refine it to get only the value, and not all of it. Guess I'll have to use some regex to find out the value. If anybody knows an easier way please share it (or if you already figured it out with the regex). Thanks!

greduan
  • 4,770
  • 6
  • 45
  • 73

2 Answers2

16

You can use the synIDattr() function to query the various attributes (like foreground and background color) of syntax highlighting. To determine the ID of the highlight group, hlID() can be used. See :help hlID():

:echo synIDattr(synIDtrans(hlID('StatusLine')), 'fg')
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

You can get the background with

:echo &background

according to :help background there should be a foreground setting, too, but it doesn't work for me.

Edit: There are the variables guibg, guifg, ctermbg and ctermfg in :hi[glight]

Huluk
  • 864
  • 6
  • 18
  • Oh no, I don't mean the option to use either a dark/light background. But rather a way to know what color the theme was using. For example how to know the exact color the theme is using for comments for example. – greduan Dec 26 '12 at 20:29
  • Oh I read the documentation you pointed to and I think that might be it, however I'm not sure. – greduan Dec 26 '12 at 20:30
  • It might be possible using the `&fg` and `&bg`, however I need to run some tests to make sure. If you find out definitely be sure to tell me about it. :) – greduan Dec 26 '12 at 20:32
  • Hmm nope, seems that isn't it. Never the less a +1. :) – greduan Dec 26 '12 at 20:36
  • Maybe you are looking for `ctermbg` and `ctermfg` in `:hi` – Huluk Dec 26 '12 at 20:40
  • Yes that's right! I'm gonna edit the question. Also for `guifg` and `guibg` as well (I can't remember if it's called exactly that). – greduan Dec 26 '12 at 20:41
  • it is, just found it and wanted to add :) – Huluk Dec 26 '12 at 20:41
  • I did find something, I put it in my main post. – greduan Dec 26 '12 at 20:54