Monday, June 30, 2014

Vim: Walk Through List of Colorschemes

Do you have a couple of favourite Vim colorschemes you frequently switch in-between? You can assign key binding to each one of them, but if you have more and prefer scrolling interface here is a simple one.

  • Add bellow snippet to your ~/.vimrc.
  • Configure colorschemes in themes list.
  • Reload your Vim configuration.
  • <Leader>th will start scroller (see key bindings in comment bellow).

" {{{ Walks through list of colorschemes (q/C-C=quit, k=prev, default=next)

function! Themes() abort
    let themes = ['luciusblack', 'hybrid', 'bclear']
    let l = len(themes)
    if !exists('s:themes_last_index')
        let s:themes_last_index = 0
    endif
    let i = s:themes_last_index
    while 1
        let theme = themes[i]
        exec 'colorscheme '.theme
        redraw | echo theme
        let c = getchar()
        if c == 3 || c == 113
            break
        elseif c == 107
            let i = i > 0 ? i-1 : l-1
        else
            let i = i < l-1 ? i+1 : 0
        endif
    endwhile
    let s:themes_last_index = i
    redraw | echo
endfunction

command! Themes call s:Themes()
nnoremap <silent> <Leader>th :call Themes()<CR>

" }}}