256 colors in vim, inside screen, in an iTerm, on OS X Leopard

Dec 15 2008

Borrowing a lot from this sage advice, I decided it’s time to figure out 256 color vim on OS X. That meant immediately leaving Terminal.app for dead, since he can’t display more than 8 (16 w/ANSI bold) colors. Here’s the alternative I came up with:

  • Installed iTerm.
  • In iTerm, hit option+cmd+B for “Profiles”.
    • Under “Keyboard Profiles, Global”, I removed all the mappings and set the “Option Key as” to “+Esc”. This allows me to switch channels in irssi with option+number.
    • Under “Terminal Profiles, Default”, I changed “Type” to “xterm-256color” and checked “Blinking cursor”.
    • Under “Display Profiles”, for “Dark Background” and “Light Background”, I tweaked the “Cursor” colors to be more visible.
  • Grabbed this perl script that prints a 256 color test palette to the console. Ran it outside of GNU Screen, and everything looked good.
  • Added the following lines to my ~/.screenrc:
#terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
# the first two are for other terminal emulators, just in case.
termcapinfo xterm "Co#256:AB=E[48;5;%dm:AF=E[38;5;%dm"
termcapinfo xterm-color "Co#256:AB=E[48;5;%dm:AF=E[38;5;%dm"
termcapinfo xterm-256color "Co#256:AB=E[48;5;%dm:AF=E[38;5;%dm"
term xterm-256color
# erase background with current bg color
defbce "on"
  • Ran the test script inside of GNU Screen, everything looked good.
  • Added the following lines to the top of my ~/.vimrc, because apparently “set t_Co=256″ must appear before any syntax and color settings:
set t_Co=256
:colorscheme railscasts
  • Started up vim inside GNU Screen, and voila!
  • Wrote about it here so I never forget this stuff.

4 responses so far

  1. [...] 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 [...]

  2. Very helpful indeed, thanks for writing this up. One note, however: it looks like the screen config is missing some important escapes. The “E[…” needs to be written as “\E[…”, or screen gets all sorts of confused. :)

    Thanks!

  3. Odd — for some reason, I had to switch to the macports version of screen. The builtin wasn’t working for me at all.

    I removed my .screenrc and .bashrc files and started from scratch too. “tput colors” still reports 256 outside of screen, 8 inside.

  4. I had this working before Snow Leopard.

    If you can’t compile screen on Snow Leopard, remove this line from pty.c:

    # include

Leave a Reply