Do you know how sometimes one thing leads to another? In my case, it started with climate change which leads to 90 degree plus days in Seattle which leads to wanted to sit outside instead of swelter indoors which leads to wanting to write some code outside which leads to trying to get better colors which leads to (finally!) understanding terminal colors. Whew that’s a lot!
Starting with 4-bit vs 8-bit vs 24-bit color
OK, bear with me on this one, but one of the fun things is to turn a fancy graphical interface like the MacOS (or iPadOS for that matter) back into a 1970s character terminal. Key to this is using the MacOS Terminal or for power uses, iTerm2.
So going back to ancient history, the original IBM PC was either monochrome or had the very cool Color Graphics Adapter (CGA). This display had sixteen colors. And back in the 1980s that was pretty amazing. Also know as 4-bit color. And if you can believe if MacOS Terminal still only supports 16 different colors. However, one advance is that if you have a modern computer is that you can actually pick which 16 colors to use in the modern 24-bit color (8 bits per color or millions of colors in Apple speak) or 40-bit color (aka High Dynamic Range or HDR or 10 bits per color or billions of colors in Apple speak).
So, when I first started using this with MacOS Terminal, the best thing to do was to get color schemes it is unintuitive, but lots of time 16-bit color is better than 256-bit color. The reason is that the 256 colors are predefined, so if you are in a real 24-bit or even 40-bit scheme, with 16-bit, you can select exactly the colors that you want.
The miracle of Solarized
This is where the wonder Ethan and his solarized color scheme comes in. He’s a graphics designer and 8 years or so ago, he designed two beautiful schemes, Solarized light and dark. The miracle is they use the same color 16 carefully chosen colors. They are quite a bit less contrasty and therefore easier to read in normal light situations. The main advantages to talk technically is that it has reduced brightness contrast but retains hue contrast so that you get good readability and not glaring bright lights.
Of course, ever character application in your system has to be told to use this, but I would suggest it as the default colors for mostly uses. There are many variations, one that I like is solarized8 which is tuned for 8-bit colors rather than the 4-bit of regular solarized.
Configuration of all this via environment variables and escape codes in MacOS Terminal and iTerm2
Now today, you set most of this with a profile page in a modern graphical application, but the way this works for character applications is way back to the 1980s. The first thing is that in a character application there is an environment variable called TERM that tells the terminal program. The way this works is that if you set TERM=xterm then this tells the program that you have true colors.
The problem is that the standard Terminal in MacOS does not have any of this support, it only works with the 16-color model. This is a good reason to install iTerm2 which by default has 24-bit color turned on.
Also the strange way that you tell a terminal application what colors to set is with escape codes, so you will programs like powerline for instance set PS1 to a bizarre sequence that looks like, “\<Esc>[38:2:” for instance. The terminal programs back then did not have any way to communicate with the computers, so by sending special sequences that you would actually tell iTerm2 what to 256 (or more depending on how COLORTERM is set) color to actually use and it by defaults set this to xterm-256color
. Note that other systems just report xterm like Ubuntu but technically this means just 8 colors. Also you can see if you are supporting more than 256 colors then you should see COLORTERM equal to 24bit or truecolor. As an aside there is a utility tput which reports what termcap thinks the colors are, but this is just set to 256 as the output to tput colors
unless you use the nonstandard TERM=xterm-direct to tell it to use the new format.
Configuration inside of vim or neovim
Well that is how you set it up in the terminal application, now it turns out that unlike graphical programs the major character applications like vi (or vim or nevoid) all have their own way of setting things up as well. The main thing is in the .vimrc file, you have a few options.
The first is that you should set for instance solarized by loading a plugin that takes care of this mess. If you are using vim-plug, then you just insert the line, Plug altercation/vim-colors-solarized
and it will load the right colors in. Then you just:
" this turns on syntax highlighting, so different colors for keywords
syntax enable
" you have the choice of light or dark
set background=dark
" here's the key to using this scheme
color scheme solarized
" Use the F5 key to switch
call toggling#map("<F5>")
Configuration of iTerm2 colors
To get this working, you can use all 24-bit colors and then you need a special configuration file called a .itermcolors file. You import these into a profile. The second trick is to use dynamic profiles, so they are stored separately as JSON, so you can get the same color layout across all your computers.
The solarized repository actually has these directly so they are easy to load up by using Preferences > Profiles > Colors > Color Presets > Import
Other Color Schemes for iTerm and Vim good for bright light
I use Solarized for just about everything but this low contrast doesn’t work well in bright sunlight. So what can you do about this, well it turns out there are many .itermcolors available and a host of vim color plugins that you can use and lots of opinions about them. Here are some:
- PaperColor. This actually uses the new Material Design colors from Google. It works with 4-, 8- and 24-bit color terminals although it really only uses 8-bits natively.
- Gruvbox. This is relatively high contrast but looks a lot like Solarized to my eye.
- Eldar. This is a dark scheme but I find it is very high contrast so nice for brighter light
Now for iterm2 you get to select different colors as well and there is entire repo filled with .itermcolors:
- Tango Light. This is actually an included color in iTerm and it is very bright which I like