zigford.org/dynamic-colorscheme-in-vim.html
2020-07-21 06:49:32 +10:00

74 lines
3.2 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="main.css" type="text/css" />
<link rel="stylesheet" href="blog.css" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="Subscribe to this page..." href="feed.rss" />
<title>Dynamic colorscheme in Vim</title>
</head><body>
<div id="divbodyholder">
<div class="headerholder"><div class="header">
<div id="title">
<h1 class="nomargin"><a class="ablack" href="http://zigford.org/index.html">zigford.org</a></h1>
<div id="description"><a href="about.html">About</a><a href="links.html"> | Links</a><a href="scripts.html"> | Scripts</a><br>Sharing linux/windows scripts and tips</br></div>
</div></div></div>
<div id="divbody"><div class="content">
<!-- entry begin -->
<h3><a class="ablack" href="dynamic-colorscheme-in-vim.html">
Dynamic colorscheme in Vim
</a></h3>
<!-- bashblog_timestamp: #201906151035.45# -->
<div class="subtitle">June 15, 2019 &mdash;
Jesse Harris
</div>
<!-- text begin -->
<p>Most folk know about <code>:colorscheme</code> in Vim. In this post I will show how I setup
my vimrc to change the colorscheme based on the time of day.</p>
<hr />
<p>This method is quite simple. It checks the time when my vimrc is loaded and
depending on the result set's the colorscheme accordingly. There may be smarter
ways to achieve this, by setting a timer to check periodically. However I find
this approach good enough for me.</p>
<p>This approach will work on Linux, macOS and Windows. In the example below, I use
the colorscheme <a href="https://github.com/gruvbox-community/gruvbox">gruvbox</a> and
simply switch between light and dark background modes.</p>
<pre><code> " Format the *nix output same as windows for simplicity
let s:time = has('win32') ? system('time /t') : system('date "+%I:%M %p"')
let s:hour = split(s:time, ':')[0]
let s:PM = split(s:time)[1]
if (s:PM ==? 'PM' &amp;&amp;
(s:hour &gt; 7 &amp;&amp; s:hour != 12)) ||
(s:PM ==? 'AM' &amp;&amp;
(s:hour &lt; 8 || s:hour == 12))
set background=dark
else
set background=light
endif
colorscheme gruvbox
</code></pre>
<p>In this example, I have dark mode enabled from 8pm until 8am. Outside these
hours, light background is set.</p>
<p>As you can see, in order to only have one method of parsing the systems
date/time, I simply use the <code>*nix</code> flexible date formatting to make it appear
similar to Windows, and then I only need 1 parsing function.</p>
<p>Tags: <a href='tag_vim-tips.html'>vim-tips</a>, <a href='tag_vim.html'>vim</a></p>
<!-- text end -->
<!-- entry end -->
</div>
<div id="footer">&copy <a href="http://twitter.com/zigford_org">Jesse Harris</a> &mdash; <a href="mailto:jesse&#64;zigford&#46;org">jesse&#64;zigford&#46;org</a><br/>
Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>
</div></div>
</body></html>