Vim Colorschemes with Putty aka. GUI vs. xterm-color256

256 color Vim in Putty

I made a big step in coding geekdom this summer by upgrading the most low-level part of my programming workflow. It started when I got frustrated with Mac keyboard shortcuts on Scandinavian keyboard layout (they Just Don’t Work for most apps), and switched to US layout in coding. Once I made that transition, I started thinking that maybe I could improve my coding speed a bit more, and see what all the fuzz is about Vim.

The greatness of Vim in coding comes from the fact that Vim has separate modes for editing text, and navigating around. While not editing, all normal keys become powerful commands, and you can do text manipulation like duplicating lines, indenting sections etc. without ever leaving this “normal mode”.

Well, Vim is great, but an additional bonus to its power is the fact that almost every *nix system has it preinstalled. So even if I’m not on my own computer, I can just launch an SSH client and use Vim to edit the piece of code I’m working on. No need to compromise. Except color schemes, which I just couldn’t get working over Putty. Today I solved that puzzle after one and half hours of googling, and thought to share the findings, maybe someone will find this the next time they face the problem.

1. Putty can support 256 colors

putty

Putty, my favorite SSH program (on Mac I can just use shell) actually has pretty good 256-color support out of the box. Just go to Window…Colors and check that you have the first three boxes checked and the two after those unchecked.

You should also go to Terminal and check that you have “Use background colour to erase screen” checked. This will avoid black (or whichever default background color you have in Window…Colours) areas in Vim where there is currently no text.

2. Vim also supports 256 colors on terminal, but not by default

To let Vim know that your terminal can do 256 colors, you can either further configure Putty or your shell to set TERM to “xterm-color256″ (e.g. export TERM=xterm-color256). Or you can just add a line to your .vimrc (the if is only for those who like to have different settings for ‘mac’ and ‘win32′):

if has('unix')
    set t_Co=256
endif

If you are in luck, you should now be all set. But chances are that you are not…

3. Most Vim color schemes don’t support 256-color terminals

The secret that most Stackoverflow answers don’t tell you is that quite a majority of Vim color schemes only define colors for GUI version of Vim, gVim. For example the “neon” scheme I like defines the normal highlight like this:

hi Normal guifg=#f0f0f0 guibg=#303030

On a terminal Vim, this does absolutely nothing, whereas the “evening” scheme works just fine – can you spot the difference?

hi Normal ctermbg=DarkGrey ctermfg=White guifg=White guibg=grey20

As you can see, the color scheme author has provided color terminal background and foreground options, too. Now one could just grab the 256 xterm colors and do a simple conversion script, but that isn’t actually necessary – there exists a working Vim script to do it automatically, CSApprox. Just unpack it to your .vim folder, or if using Pathogen, make a directory for it under .vim/bundle and restart Vim. VoilĂ !

Update: colorsupport.vim might actually be even leaner way to achieve the same thing as CSApprox.

1 Comment or trackback to Vim Colorschemes with Putty aka. GUI vs. xterm-color256

Blieque:
October 6, 2013 at 2:19

Aha! Thank-you very much. It was just the if() part that fixed it for me. I now have lovely 256 colours in Vim on my Raspberry Pi through PuTTY.

reply

Leave a Reply

Your e-mail address will not be published.


+ 7 = eleven

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>