Switching Vim Colors Revisted

This script does things a bit differently. Also, I have used rand to generate random number. So you need to have rand installed.

sudo apt-get install rand

The Script

" colorswitch.vim - Switch between installed Vim color schemes
" Author:       Rahul Janak
" Version:      0.1 (updated 21/01/2012)
" Usage:
" 		F8 Switches to next Color Scheme
"		Shift-F8 Switches to previous one
"		Alt-F8 Switches to a random one

if v:version < 700 || exists('loaded_schemes')

if !exists('g:colors_name')
		colorscheme default

let loaded_schemes = 1
let s:paths = split(globpath(&runtimepath, 'colors/*.vim'), "\n")
let g:mycolors = map(s:paths, 'fnamemodify(v:val, ":t:r")')

function! SwitchColorRandom()
		execute "let random_number = system('rand -M " . len(g:mycolors) . "')"
		execute 'colorscheme ' g:mycolors[random_number]
		execute 'colorscheme'

function! SwitchColorForward()
	    let s:idx = index(g:mycolors, g:colors_name)
		if s:idx < len(g:mycolors)
				execute 'colorscheme ' g:mycolors[s:idx + 1]
				execute 'colorscheme'

function! SwitchColorBackward()
	    let s:idx = index(g:mycolors, g:colors_name)
		if s:idx >= 1
				execute 'colorscheme ' g:mycolors[s:idx -1]
				execute 'colorscheme'

nnoremap <F8> :call SwitchColorForward()<CR>
nnoremap <S-F8> :call SwitchColorBackward()<CR>
nnoremap <A-F8> :call SwitchColorRandom()<CR>

If you don’t have many color schemes installed  then you can go to this page and download ColorSamplerPack.zip. Then extract it to ~/.vim/colors/

Next if you want to try the above script, save it in ~/.vim/plugin. Next time you open vim you will be able to access it via below mentioned shortcuts.

F8 – Test Next Scheme

Shift – F8 – Test Previous Scheme

Alt – F8 – Test a Random One

Happy Switching 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s