1. Overview

When we use the Vim editor, we may want to have a column ruler.

For example, when we edit some text in a Vim buffer, let’s suppose we want to have a length limit, say 80, on all lines. If we can have a column ruler on column 80, it can straightforwardly show how close we’re to reaching the limit as we’re typing. Also, if we view block-based data in Vim, a column ruler can help us quickly locate the lines shorter or longer than the expected length.

In this tutorial, we’ll learn how to set column rulers in the Vim editor.

2. Setting Up a Ruler on Column X

The column ruler’s called the color column in Vim. In this section, we’ll learn how to set color columns, change their color, and disable color columns.

2.1. Setting the colorcolumn Option

We can set the colorcolumn (or the short form: ‘cc’) option with the desired column number to enable the color column. For example, if we want to set a ruler on column 10 we can execute this command:

:set colorcolumn = 10

After setting this option, a red column ruler will be created on column 10:

As we can see in the demo, after we set the colorcolumn option, a red ruler immediately appears on column 10.

Further, the color column is merely a visual marker. It doesn’t influence our edit. For example, it won’t automatically wrap the lines that exceed the column.

It’s worth mentioning that the color column setting in Vim is local to the window. This means different windows can have different color column settings. Let’s see another demo to understand this quickly:

At the end of the demo, we can see that we can pass an empty value to the colorcolumn option to disable the color column. Both commands work for this purpose:

:set colorcolumn=
:set colorcolumn=""

The demos above show that Vim shows color columns in red by default. Next, let’s see how to change the ruler’s color.

2.2. Setting the Ruler’s Color

To change the color of the color column, we need to use the highlight (short form: hi) command. Here’s the highlight command’s syntax:

:highlight GROUP_NAME key=value key=value ...

The group name for the color column is ColorColumn. Two important keys to changing the ruler’s color are ctermbg and guibg.

First, ctermbg defines the ruler’s color when Vim runs in a terminal. This key accepts a color-number (color-nr):

NR-16   NR-8    COLOR NAME 
0	    0	    Black
1	    4	    DarkBlue
2	    2	    DarkGreen
3	    6	    DarkCyan
4	    1	    DarkRed
5	    5	    DarkMagenta
6	    3	    Brown, DarkYellow
7	    7	    LightGray, LightGrey, Gray, Grey
8	    0	    DarkGray, DarkGrey
9	    4	    Blue, LightBlue
10	    2	    Green, LightGreen
11	    6	    Cyan, LightCyan
12	    1	    Red, LightRed
13	    5	    Magenta, LightMagenta
14	    3	    Yellow, LightYellow
15	    7	    White

The NR-16 and NR-8 in the table above indicate 16-color and 8-color terminals.

On the other hand, guibg defines the ruler’s color when we start Vim with GUI. So we can pass a color name to guibg. It supports many color names:

Red	LightRed	DarkRed
Green	LightGreen	DarkGreen	SeaGreen
Blue	LightBlue	DarkBlue	SlateBlue
Cyan	LightCyan	DarkCyan
Magenta	LightMagenta	DarkMagenta
Yellow	LightYellow	Brown		DarkYellow
Gray	LightGray	DarkGray
Black	White
Orange	Purple		Violet

Next, let’s see a demo to change the ruler’s color to green and magenta in terminal Vim:

As we can see in the demo, although the colorcolumn setting is local to a Vim window, the color column’s highlight setting is global.

So far, we’ve learned how to define a ruler on one single column and change its color. Furthermore, Vim supports multiple rulers on different columns in the same window.

Next, let’s set up multiple rulers!

3. Setting Up Multiple Rulers

We’ve learned that :set cc=X can set up a ruler on column X. Actually, Vim’s colorcolumn option accepts a comma-separated list of columns:

Multiple rulers

As the example above shows, we’ve set three rulers on columns 3, 7, and 11.

Apart from manually passing column indexes to the colorcolumn option, we can also assign the option variable (&colorcolumn) using vim-script expressions: let &colorcolumn= …

Next, let’s see an example.

Let’s say we want to first set a ruler on column 3. Then from column 10, we need a ruler on every five columns. That is to say, we need column rulers on columns 3, 10, 15, 20, 25,  30, … .

We can achieve that by the vim-script expression:

:let &colorcolumn="3," . join(range(10, 999, 5), ",") 

Let’s walk through the command quickly and understand how it works.

In the command above, we called vim-script function range(10, 999, 5) to generate the Vim list object [10, 15, 20, 25,  … 995]. Then, the join() function converts the list into a comma-separated string. Finally, we concatenate “3,” and the converted comma-separated string using the “.” operator.

If we execute this command, we’ll get the expected color columns:

Multiple rulers by script

4. Conclusion

In this article, we’ve learned how to set up Vim color columns via the colorcolumn option. Further, we’ve explored how to set the colorcolumn option through vim-script expressions.

The default color of color columns is red. However, we’ve seen how to change the color using the highlight command.

Comments are closed on this article!