Mike Crittenden
Mike Crittenden

Reputation: 5917

Automated way to convert a vim GUI color scheme to work with 256 color vim?

I have a GUI-only vim color scheme that I would like to convert to a 256 color version. Is there an automated way to do this?

Googling only found http://www.vim.org/scripts/script.php?script_id=1809 which didn't work for me (the colors turned out completely wrong) and isn't really meant to do a conversion to be saved and reused, but rather it's for on the fly conversion.

Upvotes: 7

Views: 2249

Answers (3)

Leo
Leo

Reputation: 11

here is a simple python script that will do the conversion for you:

'''add terminal color values to a GUI only colorscheme'''

# USAGE: vim_colorscheme_convert.py <colorscheme_file>

import sys
import re

# requires path.py: https://pypi.python.org/pypi/path.py
from path import path

# requires colortrans.py: https://gist.github.com/MicahElliott/719710
from colortrans import rgb2short

HI_LINE = 'hi %(name)s guifg=%(guifg)s guibg=%(guibg)s gui=%(gui)s ctermfg=%(ctermfg)s ctermbg=%(ctermbg)s cterm=%(cterm)s'

f = path(sys.argv[1])
if not f.isfile():
    print('File does not exist: %s' % f)
    sys.exit(-1)

output = []

for line in f.lines():
    m = re.match('hi\s+(?P<name>\w+)\s+.*$', line)
    if not m:
        # append non "hi" lines verbatim
        output.append(line)

    else:
        values = {'name': m.group('name')}
        for val in ('', 'fg', 'bg'):
            m = re.search('gui%s=(?P<gui%s>\S+)' % (val, val), line)
            if not m:
                values['gui%s' % val]   = 'NONE'
                values['cterm%s' % val] = 'NONE'
            else:
                values['gui%s' % val]   = m.group('gui%s' % val)
                if not values['gui%s' % val].startswith('#'):
                    values['cterm%s' % val] = values['gui%s' % val]
                else:
                    values['cterm%s' % val] = rgb2short(m.group('gui%s' % val).strip('#'))[0]

        output.append(HI_LINE % values)

# make a back up of the original and write the new contents
f.copy('%s.bak' % f)
f.write_lines(output)

Upvotes: 1

Xavier T.
Xavier T.

Reputation: 42228

You can use CSApprox which is a plugin converting GUI color schemes to terminal color schemes.

I have tried it once or twice and the result was acceptable.

Upvotes: 7

poulter7
poulter7

Reputation: 1604

I'm using gnome-terminal and simply adding

set t_Co=256

to my .vimrc file made all the difference

Upvotes: 1

Related Questions