开发者

Vim: Set Color/Theme based off time of day

开发者 https://www.devze.com 2023-04-09 18:32 出处:网络
I have one of those super glossy monitors, so during the day I can see my own reflection better than my code on Dark themes. So I thought it\'d be great if I could have a simple if switch in my vimrc

I have one of those super glossy monitors, so during the day I can see my own reflection better than my code on Dark themes. So I thought it'd be great if I could have a simple if switch in my vimrc to set either a dark theme or a light theme based on the t开发者_运维问答ime of day.

Alas, I don't know enough about vimrc syntax and googling came up short.

Anyone wanna take a crack at this?


Something like the following should do the trick:

if strftime("%H") < 12
  set background=light
else
  set background=dark
endif

Obviously you should choose the hour based on your needs


Try the werewolf.vim plugin:

Werewolf changes your vim colorscheme automatically based on the time of day, allowing you to use a light theme during the day but "transform" to a dark theme once the sun goes down.

If you use often different color schemes, you can set several pairs of light/dark color schemes, and it will automatically switch to the matching scheme.


This is not strictly a Vim answer but you could try to install f.lux which adjusts your monitor colours to the time of the day.

Here is a demo. It has a warm yellowish glow during the day, and blueish one during the night.

Disclaimer : I have not tried it myself (but I am tempted).


You can source a .vimrc.local in your vimrc. Then you could use cron to overwrite that file according to the time of day. no scripting :-)


My night-and-day plugin is a "vim theme scheduler". You can divide the day into any number of intervals and assign a theme to each. Themes will automatically change if vim is open when a new interval begins.


I understand that this is now an old question with an accepted answer, but if you are looking to achieve the same functionality using lua instead of vimscript, here is how I accomplished it.

local hr = tonumber(os.date('%H', os.time()))
if hr > 6 and hr < 21 then -- day between 6am and 9pm
  vim.opt.background = 'light'
else -- night
  vim.opt.background = 'dark'
end

Note that this answer assumes that your preferred color scheme supports light and dark backgrounds.


I have figured out another way of changing color and background

exe 'colo' ((strftime('%H') % 18) >= 6 ? 'gruvbox' : 'kolor')
exe 'set background='. ((strftime('%H') % 18) >= 6 ? 'light' : 'dark')

Using the rest of division gives more control over the time I want to change background. We can use this shell loop to see why:

for ((i=1;i<=23;i++)){
 echo "Rest of dividing $ i by 18 equal to:  $(( $i % 18 ))"
}

If I use something like <14, instead of the rest of the division, what happens is that after midnight my color scheme changes to "light". So I make the comparison of the rest of the division of the hour by 6.

0

精彩评论

暂无评论...
验证码 换一张
取 消