Vim logo vim online Vim Book Ad

 Tip #358: Get a random colorscheme on vim startup

 tip karma   Rating -1/1, Viewed by 338 

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.
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 [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo