Emphasizing the active pane in Vim using ColorColumn

Published on May 25, 2018
under Blog

Abstract

Around a month ago I switched to using Vim full-time. I've already been using Vim for all of my command-line editing, but now I've enabled Vim emulation in all of the other editors I use (namely those coming from JetBrains).

To keep my Vim skills versatile, my Vim configuration uses vanilla key mappings. That said, I've still made a lot of quality-of-life adjustments to the config, including colour scheme tweaks. One of such tweaks is changing the style of the active pane to make it more obvious - which is also the focus of this article.

Getting it to work

There are several steps needed to get the whole thing working. Jump to the end of this article if you want to see the final code.

Initial setup

For the sake of this article, I will use red for my colour column (from here on I will call it just "column"). To make screenshots more compact I will use values 20 and 40. Let's assume I start off with this .vimrc:

Naive solution (doesn't work)

This will indeed only show the colour column in the active window, but there is one drawback - the position at which it is drawn is always 40. This defeats the purpose of using different colour column settings for different file types. We can do better!

Better solution

Instead of using one-liners with autocmd Win<Enter/Leave>, let's define some functions:

By now, you have probably guessed what we will do: We will hide the colour column in OnWinLeave() and reveal it in OnWinEnter(). Doing this is pretty straight-forward, but there are some things we have to consider.

We will "remember" the initial colorcolumn setting of a window using a window-scoped variable. Window-scoped variables are created by prefixing their names with w: (check out Vim variable scoping for more info). To remember the value, we'll need to execute the let w:initial_cc=&colorcolumn command at some point.

The question is, where do we put it? There's a catch: If we put it inside OnWinEnter(), we'll actually remember the colorcolumn of the window that we're leaving. This makes the logic somewhat complicated, so, instead, we're gonna put it inside OnWinLeave(). We're also gonna add some code to hide the colour column (since the window we're leaving will become inactive):

And we're done! Putting everything into our Vim config we'll get the desired behaviour. I know that some of my explanations lack detail - there are just too many small things to explain here. If you want to understand why (and how) this works, try implementing it yourself.

Final code

Below you can see the full Vimscript snippet that you can put into your .vimrc.json. Note that this technique can be applied to other Vim parameters too - go make something cool with it!