Better Understanding of Vim colors
Tags: vim, color, light
Published: 2021-02-21 12:33:50

I am a programmer with simple needs in regards to Vim, I need it to be Vim with and it should look nice. I've been using only basic plugins, mostly surround and some highlighting, for years now. One of the reasons for that is the fact I came from working on servers, so I don't have my `.vimrc` with me. Another factor is that I write a lot in Lua/Javascript, so there's not much I can get by autocompletion anyways.

Lately I've been working on some Typescript codebase, and a coworker was reviewing the code with me and we wanted to check the types of some object. It is not so nice without some `tsserver` to just tell us. So I've decided to go forth into the rabbit hole of Vim configurations once again.

I will skim over the TS configurations, mainly sharing some links and then go into the hightlight

There's a TL;DR at the end to get the bare gist.

## God saw that the light was good, and he separated the light from the darkness 

I use Mac at work, [Yabai](https://github.com/koekeishiya/yabai) as a tiling window manager, [Kitty](https://sw.kovidgoyal.net/kitty/index.html) as a terminal emulator, and bash as my shell.

I use terminal a lot, as a file explorer, running scripts and for text editing. I use light themes and I've been looking at my Solarized Light screen for over 3 years now. 

I've picked a theme for Kitty,
```
background            #fdf6e3
foreground            #52676f
cursor                #52676f
selection_background  #e9e2cb
color0 #e4e4e4
color8 #ffffd7
color1 #d70000
color9 #d75f00
color2 #5f8700
color10 #585858
color3 #af8700
color11 #626262
color4 #0087ff
color12 #808080
color5 #af005f
color13 #5f5faf
color6 #00afaf
color14 #8a8a8a
color7 #262626
color15 #1c1c1c
selection_foreground #fcf4dc
```

This said little to me but I liked what I saw. Sometimes some texts were white, usually when running tests, but I would just select the text and read what it said.

When configuring the TS, I've followed [this guide](https://www.vimfromscratch.com/articles/setting-up-vim-for-typescript). I've had to install NVim, [coc-tssever](https://github.com/neoclide/coc-tsserver) for that tsserver inategration and autocompletion and added some mappings

```
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gs :call CocAction("doHover")<CR>
autocmd FileType typescript,javascript,typescriptreact,javascript.jsx nnoremap <buffer> <leader>t :call CocAction("doHover")<CR>
```

Especially the gs map, that should show me a little hovering window with the type of the object the cursor is on. Here was how it looked.

![my vim is unreadable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oknkzq7d9fbrc6hyymmx.png)
 
There was this little issue that the text I wanted to read was unreadble.
I've changed the theme to something else and it was alright but something else was broken. I want that CursorColumn and CursorLine (hightlihgts the row and column) but both were just white.

And here I started reading a lot about Vim highlights and color themes and all that, I will try to pass as much information but be as short as possible.

## Off to a great adventure

I've used the ayu light theme and in the project it had a picture that looked like that

![ayu light theme](https://raw.githubusercontent.com/dexpota/kitty-themes-website/master/previews/ayu_light/preview.png)

and it's generated by this tool

```
#   Copied from http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
T='gYw'   # The test text

printf "\n         def     40m     41m     42m     43m     44m     45m     46m     47m\n";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m' '  38m' '1;38m';

  do FG=${FGs// /}
  printf " $FGs \033[$FG  $T  "

  for BG in 40m 41m 42m 43m 44m 45m 46m 47m 48m;
    do printf "$EINS \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done
echo
```

Basically it shows text colors and backgrounds, according to some 8 base colors. The column and row codes are some [Ansi codes](https://techstop.github.io/bash-script-colors/) so you can use them in your bash customizations ;)

Using this theme I got the part more readable,

![more readable theme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uaar3wtf0smwmsvxkm3o.png)

But then I wanted the cursorline and cursorcolumn to work well.

## X marks the spot

Here how it looks now,

![With Markings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x061cnc63eqbmbecn3j.png)

Notice that the column is white, and the line is just an underline.

You can notice in the picture of the theme, that the default is grayish color, and the last column is white.

I recalled that 8 is a number that I've seen here already, and indeed, it was the number of pairs in the theme config. 

According to the [Kitty configurations](https://sw.kovidgoyal.net/kitty/conf.html#color-scheme), the color scheme comes in pairs, one color for the dark variant, and one for the light variant, and the last pair is the pair for white, dark white and light white.

So I've concluded, and checked the theme that the designer decided to set the "dark white" (gray) as white, #ffffff, same as "light white" (regular white).

So I've changed color7 to #d3d3d3 and now it looks great.

![Gray :)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cv0inpnyz5690dt1wu6.png)

Regarding the line issue. I've read [here](https://vim.fandom.com/wiki/Highlighting) about changing specific colors of specific things in Vim.

Hit `:hi` to see the table of all highlight rules, 

![vim highlights](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ur4p8qf90q6j4oq5sib.png)

```
CursorColumn  ctermbg=7 guibg=Grey90
CursorLine    cterm=underline guibg=Grey90
```

The image also shows you how it would look <3. So I see that the CursorColumn has 2 values, ctermbg and guibg. Testing this a bit with different values, and reading [this thread](https://www.reddit.com/r/vim/comments/b2jj5n/differentiate_between_term_cterm_and_gui/) made me understand this:
- `guibg` is not terminal, it's probably the state that macvim or gvim runs.
- there are probably configs for `gui` similar to `cterm`
- the number in `ctermbg` is not a magic number, it's the number of the color from the theme file, i.e. the `color7` definition
- `cterm` is probable `color terminal` and it's for terminals that support 256 colors, `term` is for simpler terminals.

So just to play with it a bit more, I've added 2 more colors in my theme
```
color16               #aaedbc
color24               #a4e8b6
```

It's light green gray, 16 is dark and 24 is light, I think.

Then added this to my `nvim/init.vim` file (I miss just saying vimrc), 
```
hi CursorLine cterm=bold ctermbg=16 guibg=DarkGrey
```

and now it looks like this

![Green glory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64cu3l72k6yse2bqjepk.png)

## TL;DR

Basically,
- Themes are basically setting some env. variables, color1-16, that set the color definitions
- they come in pairs, 0+8, 1+9 etc, when the lower is the dark variant and the high is the light variant, e.g. color0+8 are black, 1+9 are red, 2+10 are green. [Further reading](https://sw.kovidgoyal.net/kitty/conf.html#the-color-table)
- you can run the script given above to generate a table of all color configurations for the basic 8, and see if everything is readable and makes sense to you.
- Vim uses these colors in it's hightlight definition
- Run `:hi` to see your defintions, you will need to find the configuration you want to change, like CursorLine for me
- the `ctermbg` setting sets the background in color terminals, and the value is the number of color that corresponds to the env. variable, i.e. if you set there 7, it will take the variable `color7`.