vi/vim: How to control/configure editor colors (color settings)

vim colors FAQ: Can you provide details on how to control/configure colors in the vim editor (i.e., vim color settings)?

Sure. When using vim syntax highlighting, a common complaint is that the default color scheme is a little too bold. In this article I'll try to demonstrate how you can change the colors in vim to be a little more pleasing, or at least be more in your control.

Note: This tutorial is about how to manually configure vim color settings. If you’re interested in choosing a vim color scheme, or figuring out which colorschemes are available on your system, please see my vi/vim color scheme (colorscheme) tutorial.

How to configure vim color settings

You can control your vim color settings in your vim startup file. On older Unix systems the vi configuration file was named .exrc, and on modern systems it is named .vimrc. Either file will be located in your home directory on a Unix or Linux system.

The next thing to know is that you control the vim colors using a command named highlight. The highlight commands are powerful, you need a little bit of background information to learn how to use them. Let’s look at a sample command to help us get rolling:

highlight Normal ctermbg=Blue

This highlight command can be read as “When using a color terminal (cterm), set the background terminal color (ctermbg) to Blue for the ‘Normal’ group.”

At first blush the word “Normal” may not make much sense, but if I add in a few more groups, I think you’ll see the power of groups:

highlight Comment ctermbg=DarkGray
highlight Constant ctermbg=Blue
highlight Normal ctermbg=Black
highlight NonText ctermbg=Black
highlight Special ctermbg=DarkMagenta
highlight Cursor ctermbg=Green

" this next line is needed to enable your custom colors:
syntax enable

Because vim supports syntax highlighting, it makes sense that groups like these need to be specified. For instance, imagine you’re editing a Java file. If you’re familiar with Java -- or any other programming language -- you’ll know that it consists of things like comments, constants, variables, and many more things. Therefore, you need a way to specify these segments of code, and the “product” portion of the highlight command lets you specify these regions.

Before moving on to some more complicated examples, note that I included the syntax enable command at the end of that example. You’ll need this command to get your custom vi colors to work, so I thought I’d slip it in now so you can start experimenting with colors on your own system.

How to specify vim foreground and background colors

Before we look at the general syntax of the vi highlight command, lets take a look at a slightly more complicated example:

highlight Comment ctermbg=Blue ctermfg=White

This example shows demonstrates that you can specify both a foreground color (ctermfg) and a background color (ctermbg) with one command.

How to add other vim font attributes

To take this one step further, you can also add some additional font attributes with the cterm keyword. For instance, in addition to adding color to comments, if I want to underline them as well I can use this command:

highlight Comment cterm=underline ctermbg=Blue ctermfg=White

Now that you’ve seen a complete vim color command like this, as you might guess, the general version of the highlight command looks like this:

highlight Group font-key-value-terms ...

Now that you’ve seen the general vim color command syntax, we can look at the possible values for each key-value field.

Possible highlight Groups

The list of vim Groups for syntax highlighting is actually pretty large, so I’m not going to try to include them all here. As you’ve seen in the examples so far, some possible Groups are:

Comment
Constant
Normal
NonText
Special
Cursor

To see a long list of all possible vim highlight groups, search this vim documentation page for the phrase “*highlight-groups*”, and you’ll find many more groups, like these:

Cursor
CursorLine
ErrorMsg
Folded

and many more.

possible vim highlight keys (related to fonts and colors)

So far I’ve only looked at three possible vim highlight command font keys: cterm, ctermfg, and ctermbg. These all work on character-based terminals with color support. Although I haven’t tried the other terms, you can also use term for terminals that have no color support (though I have no idea what this actually does), and you can use gui, guifg, and guibg to work with GUI versions of vim, which I assume means gVim. (Again, sorry, I haven’t tried this, but I believe that is correct.)

When using a key like cterm, you can use these values:

bold
underline
reverse
italic
none

To be clear, don't try to use color values with the cterm key -- you’ll just get an error message.

vim ctermfg and ctermbg color values

As shown above, you can use color values with the ctermfg and ctermg keys. This list is a little shorter than the possible list of Groups, so I’ve copied the list of possible values here from the vi documentation page that I linked to earlier:

*cterm-colors*

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

If you’re using gvim you should visit that page for more information. It looks like you can use the convential RGB syntax (like “#FFCC99”) to specify colors in a GUI environment.

Summary: vi and vim color settings

Whew, that seems like a lot of “vi and vim color” ground to cover in a short time -- I hope it all makes sense. If you have any questions, comments, or vi color tips to share, feel free to use the comment form below.

As a final note, if you’re new to the concept of syntax highlighting in vi, here’s a link to my vim syntax highlighting tutorial.