The One and the Many

A better terminal vim colour theme

My editor for years has been gvim, the GUI version of vim. I forget why I started using it over terminal vim, but these days I've stuck with it for its superior support for colours. Since I've felt it necessary to use terminal vim when working from a Chromebook, I decided to see if I could get my theme working in it.

I discovered that the two versions of vim support colours in different ways: In terminal vim you can use at most 256 colours, declared using numbers e.g. ctermfg=123. In gvim you can use hex colours e.g. guifg=#f0f0f0.

I found that many themes, including the one I've grown to love, proton.vim, are written to support only GUI colours. These themes are not compatible with terminal vim.

I really wanted something as close as possible to my current theme. What could I do?

I first thought I could translate each colour to something close. Doing that manually was not easy and I gave up.

There are apparently ways to automatically do this, but the 3 solutions I found on that page didn't work for me. This was possibly because of a lack of true colour support in my terminal (more on that below), but I gave up on this.

I discovered a website, vimcolors.com, that lets you preview themes. It lets you specify whether you want to use the theme in terminal mode as well as whether you like light or dark themes (I like light). I tried out a few themes but they were all going to be very different from proton.vim.

While looking at one of them, vim-open-color, I saw mention of converting colours using an algorithm, CIEDE2000. That seemed a promising way to take my current theme and convert it to something approximately the same. vim-open-color also includes a gist that pointed me to a Ruby gem implementing this algorithm.

I wrote a program to convert my proton.vim to proton-cterm.vim. Using it still looked nothing like the gvim version though.

I came across this gist (from the vim manual) about true colour support in various terminals and programs. I found that the X terminal I've been using, urxvt, doesn't support true colours. I tried st and miraculously terminal vim looked almost identical to gvim.

I poked at its settings for a bit, but having to edit C code and recompile to change them got old. I tried an st fork called xst that lets you change settings via Xresources. That worked well, so I've now switched over to using it!

Here's a screenshot showing what it looks like:

Screenshot of my terminal vim

Anyway terminal vim now looks close to gvim for me. I'm happy.

Update: I found that my theme looked good in urxvt when using Ubuntu 18.04. I discovered that there $TERM was set to rxvt-unicode-256color whereas in Debian stretch it was rxvt-unicode. In Debian to get 256 colour support with urxvt you have to install the rxvt-unicode-256color package instead. I tried this and vim looked good, so I decided to switch back to urxvt. Despite this not being full true colour support, I otherwise have urxvt set up how I like it.

Appendix: More on gvim vs. vim

In the past another reason I preferred gvim was its support for files in different tabs, in particular switching between them with alt+number. By default terminal vim doesn't support this, but I eventually figured it out (see my .vimrc).

I remember also liking being able to resize panes with the mouse, though I rarely need that.

One thing I haven't figured out is how I'll deal with copy and pasting outside vim. With gvim I can copy to the regular X paste buffer (save to the special vim buffer +). With a remote terminal vim this won't be possible. Although this comparison is unfair as local terminal vim works the same.

The main negative with gvim (other than using it from a Chromebook likely being a non-starter) is that using it via X forwarding is not pleasant. You definitely notice input latency. In the past how I worked around this was copying files locally back and forth, which is obviously painful. But on some projects I used to do this regularly since I felt less efficient with terminal vim.

Tags: vim, programming

Comments