Full 256 color support for VIM and/or Xterm on Ubuntu 12.04

TL;DR

Put set t_Co=256 at the top of your ~/.vimrc file and/or export TERM="xterm-256color" at the bottom of your .bashrc file. If you have the former, then you don’t need the latter. If you have the latter, then you don’t need the former, but it doesn’t hurt to have both. </TL;DR>

If you want you want full 256 color support in your terminal and/or VIM editor on Ubuntu 12.04, then you came to the right place. Hopefully you didn’t spend nearly as much time as I have trying to figure out this nagging issue because virtually everything I have read via numerous Google searches has been wrong! In the end this whole ordeal I went through turned out to be a giant face-palm moment when I realized the fix was easier than I thought!

Now, I think most people that would ever care to have 256 color support want it for the same reason I wanted it, to apply pretty color schemes to the VIM editor running within the terminal. However, some people might just want tmux to run 256 color support, not a problem. So with a Google search the first thing I run across was [SOLVED] VIM 256 Color Support Config Xdefaults – Ubuntu 12.04 and down the rabbit hole I went. I’m going to get to the point, but let me just say you don’t need to make or configure Xdefaults, xsession, and you don’t need to install ncurses-term or recompile xterm from source with special configure options like --xterm-256color etc. </facepalm>

Want VIM to open in terminal with 256 color support? Put set t_Co=256 in your ~/.vimrc file, that’s it!. If you don’t have the file, then create it. Also, you will want to put that setting at the top of the file so it executes before your color scheme.

WAIT you say! Commands like tput colors and echo $TERM tell me I only have 8 color support. That’s fine and they’re probably right, but as far as I can tell when you execute VIM that set t_Co=256 setting will tell your terminal emulator to run in 256 color mode. And if you’re running Ubuntu 12.04, then xterm comes with 256 color mode support by default.

Regardless of your predicament, I’m going to at least give you the tools to test your color support from within terminal and within VIM itself.

These two Perl scripts are a good way of checking to see if your terminal has 256 color capabilities. There’s colortest from vim.org and there’s 256colors2.pl which I found over at www.frexx.de. Here is the source code for the latter in case the link breaks in the future:

#!/usr/bin/perl
# Author: Todd Larason <jtl@molehill.org>
# $XFree86: xc/programs/xterm/vttests/256colors2.pl,v 1.2 2002/03/26 01:46:43 dickey Exp $

# use the resources for colors 0-15 - usually more-or-less a
# reproduction of the standard ANSI colors, but possibly more
# pleasing shades

# colors 16-231 are a 6x6x6 color cube
for ($red = 0; $red < 6; $red++) {
    for ($green = 0; $green < 6; $green++) {
	for ($blue = 0; $blue < 6; $blue++) {
	    printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
		   16 + ($red * 36) + ($green * 6) + $blue,
		   ($red ? ($red * 40 + 55) : 0),
		   ($green ? ($green * 40 + 55) : 0),
		   ($blue ? ($blue * 40 + 55) : 0));
	}
    }
}

# colors 232-255 are a grayscale ramp, intentionally leaving out
# black and white
for ($gray = 0; $gray < 24; $gray++) {
    $level = ($gray * 10) + 8;
    printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\",
	   232 + $gray, $level, $level, $level);
}

# display the colors

# first the system ones:
print "System colors:\n";
for ($color = 0; $color < 8; $color++) {
    print "\x1b[48;5;${color}m  ";
}
print "\x1b[0m\n";
for ($color = 8; $color < 16; $color++) {
    print "\x1b[48;5;${color}m  ";
}
print "\x1b[0m\n\n";

# now the color cube
print "Color cube, 6x6x6:\n";
for ($green = 0; $green < 6; $green++) {
    for ($red = 0; $red < 6; $red++) {
	for ($blue = 0; $blue < 6; $blue++) {
	    $color = 16 + ($red * 36) + ($green * 6) + $blue;
	    print "\x1b[48;5;${color}m  ";
	}
	print "\x1b[0m ";
    }
    print "\n";
}

# now the grayscale ramp
print "Grayscale ramp:\n";
for ($color = 232; $color < 256; $color++) {
    print "\x1b[48;5;${color}m  ";
}
print "\x1b[0m\n";

To get the source code first expand it and then click “view source” in the top right hand corner. Put that in a text file and save it as 256colors.pl or whatever you like. To run the script type perl 256colors.pl from your terminal (assuming your in the same directory as the script). Of coarse you can just download one or both of those scripts and execute them the same way. For the second script (256colors.pl) the output should look like this if you have 256 color capabilities: 256 colors

Now let me be clear, both of those scripts will only test for the terminal’s 256 color capabilities. In other words, they both tell your terminal to turn on 256 colors for the test. Essentially that means that xterm was compiled or installed properly on your operating system with the 256 color configuration options. Again if you have Ubuntu 12.04, then this shouldn’t be a problem for you. However, that does not mean that your terminal is running at 256 colors all the time. Xterm 256 color capabilities might be installed, but they are not enabled by default. This is why if you try to run a 256 color scheme like lucius in vim without the set t_Co=256 setting in your .vimrc file, you’re going to have a bad time.

You can test whether your terminal is running in 256 mode or not a couple of ways. The first is to simply check the output of those two commands I mentioned earlier tput colors and echo $TERM. The output for the first should say 256 and for the second xterm-256color. However, I have come to not trust those outputs (for the wrong reasons) and found a way to test it directly.

I stumbled upon this python script that happens to test the color output of terminal without telling the terminal what color mode to run in. For the life of me I can’t find a direct link to the script or even the version of the source code I’m talking about. However, this is a link to the newer version of the same script that does the same thing as the ones above. It tells the terminal to run 256 mode and runs a color test. If you want to test your terminal in the way I’m suggesting, then you’re going to have to copy the source (below) and save it as a .py text file. Then run it from your terminal with the following command python filename.py. This is assuming you have python installed; most Linux distributions come with it by default.

#!/usr/bin/env python
# Copyright (C) 2006 by Johannes Zellner, <johannes@zellner.org>
# modified by mac@calmar.ws to fit my output needs
# modified by crncosta@carloscosta.org to fit my output needs

import sys
import os

def echo(msg):
    os.system('echo -n "' + str(msg) + '"')

def out(n):
    os.system("tput setab " + str(n) + "; echo -n " + ("\"% 4d\"" % n))
    os.system("tput setab 0")

# normal colors 1 - 16
os.system("tput setaf 16")
for n in range(8):
    out(n)
echo("\n")
for n in range(8, 16):
    out(n)

echo("\n")
echo("\n")

y=16
while y < 231:
    for z in range(0,6):
        out(y)
        y += 1

    echo("\n")

echo("\n")

for n in range(232, 256):
    out(n)
    if n == 237 or n == 243 or n == 249:
        echo("\n")

echo("\n")

Now if you want your terminal to always have 256 color support enabled by default, then put export TERM="xterm-256color" in your .bashrc file. I read somewhere that you should put it at the bottom of the file (don’t as me why). That’s what I did and it works fine.

Finally, I went through a very confusing time when using a vim script to cycle through color schemes and one of them told me that I couldn’t load it because I couldn’t do 88 / 256 colors or something. I started to question whether I really was in 256 color mode in VIM or not. Being the type of person that needs evidence; I went looking for a way to test if VIM could output 256 colors or not and I found it! Here is a link to the source, but I had to modify the script slightly for it to display 256 colors. Also, I cut out the GVim test part of the script in part because I couldn’t get it to work and Gvim has its own color testing functions. The source for the modified script is below.


" Color test: Save this file, then enter ':so %'
" Then enter the following commands:
"   :VimColorTest    "(for console/terminal Vim)
function! VimColorTest(outfile, fgend, bgend)
  let result = []
  for fg in range(a:fgend)
    for bg in range(a:bgend)
      let kw = printf('%-7s', printf('c_%d_%d', fg, bg))
      let h = printf('hi %s ctermfg=%d ctermbg=%d', kw, fg, bg)
      let s = printf('syn keyword %s %s', kw, kw)
      call add(result, printf('%-32s | %s', h, s))
    endfor
  endfor
  call writefile(result, a:outfile)
  execute 'edit '.a:outfile
  source %
endfunction
" Increase numbers in next line to see more colors.
command! VimColorTest call VimColorTest('vim-color-test.tmp', 1, 256)

Copy the source (above) into a text file like VimColorTest.vim and for ease you can put it in your ~/.vim/plugin directory (hint: if you don’t have any plugins already, then you might have to create this directory). If you put it in your plugin directory, then fire up vim and execute the script by typing :VimColorTest. If you did not want to put this in your plugin directory, then source the script first :source ~/Downloads/VimColorTest.vim (directory path is just an example) and then execute :VimColorTest. With these simple color test you can be sure you have 256 color support or not.

Advertisements

One response to “Full 256 color support for VIM and/or Xterm on Ubuntu 12.04

  1. For some reason, this solved a weird problem I had in Vim on Archlinux while trying to use a colorscheme that used 256 colors.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s