Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #358 - Set a random color scheme at startup

Created: November 4, 2002 3:31 Complexity: intermediate Author: Madoka Machitani Version: 6.0 Karma: 11/6 Imported from: Tip#358

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 & 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')

Comments

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.

madokam--AT--zag.att.ne.jp , November 4, 2002 3:44


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


madokam--AT--zag.att.ne.jp , October 14, 2003 3:53


It's better to add redraw echo g:colors_name at the last.

Anonymous , December 30, 2004 7:43


Advertisement