Indelible InkFresh Ink (Vim)2024-02-06T16:01:44+00:00https://www.indelible.org/ink/Jon PariseCopyright 1999-2024 by Jon Parise. All rights reserved.Vim Color Schemes2009-04-21T00:00:00+00:00https://www.indelible.org/ink/vim-colorschemesJon Parise<p>The <a href="http://www.vim.org/">Vim</a> text editor supports highly-configurable color schemes which build
upon the editor’s rich syntax highlighting system. The stock Vim distribution
includes a number of color schemes, and many more are available from the <a href="http://www.vim.org/scripts/script_search_results.php?keywords=&script_type=color+scheme&order_by=rating&direction=descending&search=search">Vim
Scripts repository</a>.</p>
<p>Color scheme definitions are simply normal Vim scripts that live in the
<code class="language-plaintext highlighter-rouge">colors/</code> directory of the Vim runtime hierarchy (see <code class="language-plaintext highlighter-rouge">:help runtimepath</code>).</p>
<p>Color schemes are loaded using the <code class="language-plaintext highlighter-rouge">:colorscheme</code> command. The scheme’s name
is determined by the filename of its script file (minus the <code class="language-plaintext highlighter-rouge">.vim</code> extension).
For example, to load the stock <em>blue</em> color scheme (which is defined by the
<code class="language-plaintext highlighter-rouge">colors/blue.vim</code> script):</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="p">:</span><span class="k">colorscheme</span> blue</code></pre></figure>
<h2 id="creating-color-schemes">Creating Color Schemes</h2>
<p>Creating a custom color scheme is quite easy. Start by creating a new Vim
script file in the <code class="language-plaintext highlighter-rouge">colors/</code> directory based on the name of the new scheme.
Start the script with the following commands:</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="k">set</span> <span class="nb">background</span><span class="p">=</span><span class="nb">dark</span>
<span class="c">"or background=light</span>
<span class="nb">highlight</span> clear
<span class="k">if</span> <span class="nb">exists</span><span class="p">(</span><span class="s2">"syntax_on"</span><span class="p">)</span>
<span class="nb">syntax</span> reset
<span class="k">endif</span>
<span class="k">let</span> <span class="nv">g:colors_name</span> <span class="p">=</span> <span class="s2">"example"</span></code></pre></figure>
<p>These commands will reset the syntax highlighting system to its default state.
Note that some color scheme scripts might prefer a light background, so that
first line should be changed accordingly. (<code class="language-plaintext highlighter-rouge">highlight clear</code> uses the
<em>background</em> value, so <em>background</em> must be set first.)</p>
<p>The final line sets the global <code class="language-plaintext highlighter-rouge">colors_name</code> variable to the scheme’s name
(<em>example</em>, in this example).</p>
<p>The rest of the script defines the color scheme itself. This is accomplished
primarily through the <code class="language-plaintext highlighter-rouge">highlight</code> (or <code class="language-plaintext highlighter-rouge">hi</code>) command. Each <code class="language-plaintext highlighter-rouge">highlight</code> command
sets the colors for a single syntax group. Setting the colors for the
<em>Comments</em> group might look like this:</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="k">hi</span> Comment ctermbg<span class="p">=</span>black ctermfg<span class="p">=</span>darkgrey guibg<span class="p">=</span><span class="mh">#000000</span> guifg<span class="p">=</span><span class="mh">#777777</span></code></pre></figure>
<p>To see the full list of Vim’s syntax groups (along with their current
highlight settings), run the following command from within the editor:</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="p">:</span><span class="k">source</span> $VIMRUNTIME<span class="sr">/syntax/</span>hitest<span class="p">.</span><span class="k">vim</span></code></pre></figure>
<h2 id="testing-runtime-features">Testing Runtime Features</h2>
<p>Because the color scheme is simply a Vim script, you can conditionalize the
definitions based on various runtime values. The presence of the
<code class="language-plaintext highlighter-rouge">gui_running</code> feature indicates that the Vim GUI is running, for example:</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="k">if</span> <span class="nb">has</span><span class="p">(</span><span class="s1">'gui_running'</span><span class="p">)</span>
<span class="c">" GUI colors</span>
<span class="k">else</span>
<span class="c">" Non-GUI (terminal) colors</span>
<span class="k">endif</span></code></pre></figure>
<p>And the terminal’s color range – the number of available colors – can be
queried via the <code class="language-plaintext highlighter-rouge">&t_Co</code> variable:</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="k">if</span> &<span class="nb">t_Co</span> <span class="p">></span> <span class="m">255</span>
<span class="c">" More than 256 colors are available</span>
<span class="k">endif</span></code></pre></figure>
<h2 id="additional-configuration">Additional Configuration</h2>
<p>Color scheme scripts can support basic configuration using global variables.</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="k">if</span> <span class="nb">exists</span><span class="p">(</span><span class="s2">"g:example_force_dark"</span><span class="p">)</span>
<span class="k">set</span> <span class="nb">background</span><span class="p">=</span><span class="nb">dark</span>
<span class="k">endif</span></code></pre></figure>
<p>The user should set this variable in his <code class="language-plaintext highlighter-rouge">.vimrc</code> file before loading the
color scheme script.</p>
<figure class="highlight"><pre><code class="language-vim" data-lang="vim"><span class="k">let</span> <span class="nv">g:example_force_dark</span> <span class="p">=</span> <span class="m">1</span>
<span class="k">colorscheme</span> example</code></pre></figure>
<p>(Global variables can be “unset” using the <code class="language-plaintext highlighter-rouge">unlet</code> command.)</p>