sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #358: Get a random colorscheme on vim startup

 tip karma   Rating 11/6, Viewed by 2532 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   November 4, 2002 3:31      complexity:   intermediate
author:   Madoka Machitani      as of Vim:   6.0

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

Copy & paste 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')
" Make backward slashes forward if necessary
if (has('dos16') || has('dos32') || has('win16') || has('win32') || has('os2')) && !&shellslash;
  let s:colors = substitute(s:colors, '', '/', 'g')
endif

if strlen(s:colors)
  " If there are two or more colorschemes
  if s:colors =~ ','
    let s:rnd = matchstr(localtime(), '..$') + 0
    let s:loop = 0
    " Rotate the list s:rnd times
    while s:loop < s:rnd
      let s:colors = substitute(s:colors, '^\([^,]\+\),\(.*\)$', '\2,\1', '')
      let s:loop = s:loop + 1
    endwhile
  endif
  " Obtain the filename of the first colorscheme in the list.  e.g.:
  "   c:/home/vimfiles/colors/foo.vim
  " Then, trim unecessary parts to get this:
  "   foo
  let s:color = substitute(matchstr(s:colors, '^[^,]\+'), '^.*/\(.*\)\.vim$', '\1', '')
  execute "colorscheme" s:color
endif
unlet! s:colors s:color s:rnd s:loop


Please be noted that adding this script might slightly slow down the startup
time of vim.  Enjoy!

 rate this tip  Life Changing Helpful Unfulfilling 

<< Adding a console to gdbvim | Download RedHat RPMS of the latest and greatest version of VIM. >>

Additional Notes

[email protected], November 4, 2002 3:44
Ah!  A backward slash has disappeared on the script line 5.

Please correct the line
  let s:colors = substitute(s:colors, '', '/', 'g')

to:
  let s:colors = substitute(s:colors, "\\", '/', 'g')

Sorry.
[email protected], October 14, 2003 3:53
I've made a slight improvement:

" 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
Anonymous, December 30, 2004 7:43
It's better to add
redraw
echo g:colors_name
at the last.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo