How to get the list of all installed color schemes in Vim?

前端 未结 11 863

Is there a way to get a list of all installed color schemes in Vim? That would make very easy to select one without looking at the .vim directory.

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 17:37

    Looking at my system's menu.vim (look for 'Color Scheme submenu') and @chappar's answer, I came up with the following function:

    " Returns the list of available color schemes
    function! GetColorSchemes()
       return uniq(sort(map(
       \  globpath(&runtimepath, "colors/*.vim", 0, 1),  
       \  'fnamemodify(v:val, ":t:r")'
       \)))
    endfunction
    

    It does the following:

    1. Gets the list of available color scheme scripts under all runtime paths (globpath, runtimepath)
    2. Maps the script paths to their base names (strips parent dirs and extension) (map, fnamemodify)
    3. Sorts and removes duplicates (uniq, sort)

    Then to use the function I do something like this:

    let s:schemes = GetColorSchemes()
    if index(s:schemes, 'solarized') >= 0
       colorscheme solarized
    elseif index(s:schemes, 'darkblue') >= 0
       colorscheme darkblue
    endif
    

    Which means I prefer the 'solarized' and then the 'darkblue' schemes; if none of them is available, do nothing.

提交回复
热议问题