Search code examples
vimcolor-scheme

Defining a color variable in vim


When, for example, making a colorscheme, how can one define #40ffff as "UglyColor" (i.e. as a variable) ?

Possible / not possible ?


Solution

  • It's not possible with the built in syntax. However, it can be done if the you make your own syntax:

    let UglyColor = '#40ffff'
    let Greenish  = '#00dd00'
    let MyStyle   = 'bold'
    exe 'hi Keyword gui=' . MyStyle . ' guifg=' . UglyColor
    exe 'hi Comment guifg=' . Greenish
    

    You could then take this further by creating a dictionary:

    let UglyColor = '#40ffff'
    let Greenish  = '#00dd00'
    let ColourAssignment = {}
    let ColourAssignment['Keyword'] = {"GUIFG": UglyColor, "GUI": "Bold"}
    let ColourAssignment['Comment'] = {"GUIFG": Greenish}
    

    And then process it with something like this:

    for key in keys(ColourAssignment)
        let s:colours = ColourAssignment[key]
        if has_key(s:colours, 'GUI')
            let gui = s:colours['GUI']
        else
            let gui='NONE'
        endif
        if has_key(s:colours, 'GUIFG')
            let guifg = s:colours['GUIFG']
        else
            let guifg='NONE'
        endif
        if has_key(s:colours, 'GUIBG')
            let guibg = s:colours['GUIBG']
        else
            let guibg='NONE'
        endif
        if key =~ '^\k*$'
            execute "hi ".key." term=".term." cterm=".cterm." gui=".gui." ctermfg=".ctermfg." guifg=".guifg." ctermbg=".ctermbg." guibg=".guibg." guisp=".guisp
        endif
    

    This is how my Bandit colour scheme works (with a bit more logic in there for auto-generating cterm colours, light background colours and a syntax file so that the colour scheme self-highlights). Feel free to have a look at that one and steal the functions and format for your own colour scheme.