256 colors in vim on OS X

Terminal colors are broken out of the box in OS X terminals. In part due to a poor OS X terminal implementation, part due to problems with vim. I try new colorschemes and get strange, random looking results.

After many fruitless searches I finally got 256 colors working in vim in a terminal. This post and the GVim site were very helpful. It turns out that the included Terminal app in OS X only supports 16 colors. The terminal background can be set to any color which confuses the issue. The colortest Perl script available on the Gvim site makes this clear.
OS X Terminal colortest:
colortest run in Terminal

The free GNU licensed iTerm terminal does support 256 colors.
iTerm colortest:
colortest run in Terminal

OK, now the terminal supports 256 colors. There are still some hoops to jump through to get vi to show colors though. Most colorschemes are built for gui (using 24-bit RGB colors), so vim needs to be built with gui support. This can be checked with :echo has(’gui’). It reports 1 for yes, 0 for no. The vim I had (default or Fink installed) didn’t have gui support, so I downloaded GVim and copied it over the old vim:

cp /Volumes/Vim-7.2.148/Vim.app/Contents/MacOS/Vim /usr/bin/vim

and now vim has gui support.

I copied /usr/share/vimrc to /usr/bin because the downloaded vim didn’t know to look in /usr/share and added a few configuration lines:

” Doesn’t get set right.
set t_Co=256

” Needed because the new vim doesn’t know to look in /usr/share
set runtimepath=~/.vim,/usr/share/vim/vim72_2,/usr/bin/vimfiles,/usr/bin,/usr/bin/vimfiles/after,~/.vim/after
let $VIMRUNTIME=’/usr/share/vim/vim72_2′

syntax on
colorscheme LightTan

OK, so now 256-color support works and I can set colorschemes. They still don’t look right because colorschemes made for the gui don’t look right on a 256-color terminal. There’s a great vim script called CSApprox that converts gui colors to the closest of the 256 colors and makes gui colors look pretty good in a terminal. Download it, unzip it, and copy files to the vim directory:
sudo cp plugin/CSApprox.vim /usr/share/vim/vim72_2/plugin
sudo cp autoload/csapprox.vim /usr/share/vim/vim72_2/autoload

and now when I load colorschemes they look pretty close to what the Vim colorscheme test site shows, with the only difference due to font differences.

To get full color support when I log into a linux machine I need to do some more setup. To get gui support in terminal:
yum install vim-X11
and then add to /etc/profile:
alias vi=’gvim -v’
and “source /etc/profile” to enable it.
and add:

set t_Co=256

if &t_Co == 256
colorscheme LightTan

to /etc/vimrc to force 256-color mode and pick a colorscheme.

I also copied the CSApprox files to /usr/share/vim/vim70 and now it works when I ssh to this machine as well!

2 Responses to “256 colors in vim on OS X”

  1. Agree Guy Says:

    After switching from a linux dev environment I quickly discovered that iTerm was necessary to get >16 colors in a term on leopard.

    Unfortunately if you do any sort of serious development in a term, you’ll quickly come to realize that the CPU hit of even the latest iTerm is too much to justify the pretty factor you get for free on other unices.

    Please fix this apple (in Leopard, not Snow Leopard!)

  2. tayfun Says:

    I also recently switched to Mac from Linux environment like the previous poster, due to a job change. Now I’m missing my development environment on Linux which I think was much better one anyway. Apple needs a decent package manager (not fink or macports which is community oriented and shallow) that can handle many programs needed for development.

    Mac switch has been a disappointment for me in terms of development.

Leave a Reply