Vim Tips Wiki
Advertisement
Tip 358 Printable Monobook Previous Next

created November 4, 2002 · complexity intermediate · author Madoka Machitani · version 6.0


This script picks a colorscheme randomly among all available schemes files when vim starts up. This is similar to VimTip341, but differs in that it is independent of other script liblaries, besides the randomness.

Copy the lines below to somewhere appropriate in your vimrc:

" Create the comma-separated list of colorscheme files
let s:colors = substitute(globpath(&runtimepath, 'colors/*.vim'), '\n', ',', 'g')
if strlen(s:colors)
  " Count the number of color schemes
  let s:num = strlen(substitute(s:colors, '[^,]\+', '', 'g')) + 1
  if s:num > 1
    let s:loop = localtime() % s:num
    " Rotate the list s:loop times
    while s:loop
      let s:colors = substitute(s:colors, '^\([^,]\+\),\(.*\)$', '\2,\1', '')
      let s:loop = s:loop - 1
    endwhile
  endif
  let s:color = matchstr(s:colors, '^[^,]\+')
  unlet! g:colors_name
  execute 'source' s:color
  " Prevent the message from disappearing
  redraw
  echomsg 'Color applied: '.(exists('g:colors_name') ? g:colors_name : '').' ('.s:color.')'
endif
unlet! s:colors s:color s:num s:loop

Adding this script might slightly slow down the startup.

Comments

It's better to add

redraw
echo g:colors_name

at the last.


Advertisement