Time of day as conditional in Vim

7

3

I'm working on improving my .vimrc, and I want to have command inside of it that depends upon whether it is night or day. I'd like to have something like the following in the end:

if isNightTime
  " do something
else
  " some other thing
endif

What's the best way to go about this?

Jonathan Sterling

Posted 2011-04-03T18:45:48.927

Reputation: 213

1Have you seen :help time-functions ? – Raimondi – 2011-04-03T19:15:35.537

Guess I hadn't seen that. I'm sorry for bothering folks before I'd sufficiently studied the documentation! – Jonathan Sterling – 2011-04-03T23:04:07.870

1I didn't mean it that way, Vim's help can be a bit difficult to work with at first, that's why I pointed that page to you ;) – Raimondi – 2011-04-04T03:55:56.417

Didn't take any offense! :) Thanks, everyone, for being so helpful. – Jonathan Sterling – 2011-04-04T04:39:04.943

Just out of curiosity, what are you doing that requires this type of switch? Is it related to a colorscheme? – Matt Alexander – 2011-05-03T16:15:48.847

@mattalexx Yeah, I'm using the Solarized colorscheme, which has dark and light variants.

– Jonathan Sterling – 2011-05-03T17:54:20.437

Answers

6

Using the strftime() funtion is probably the best approach. If you are satsified with one-hour resolution, you could do something like this:

let hour = strftime("%H")
if 6 <= hour && hour < 18
    " do daytime stuff
else
    " do nighttime stuff
endif

See

:help strftime()

and the strftime man page.

garyjohn

Posted 2011-04-03T18:45:48.927

Reputation: 29 085