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')
	finish
endif

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

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]
		redraw
		execute 'colorscheme'
endfunction

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

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

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 🙂

Advertisements

Switching Colorschemes in Vim

If you have lots of vim color schemes and you want to check each one out, then, I don’t think anything could be better than this. Check it out.

http://vim.wikia.com/wiki/Switch_color_schemes

If you are unsure of how to use it, first save the file ( Found below the Script Section or download it from here) into your plugin directory  ~/.vim/plugin. If it’s not there, make one.

mkdir -p ~/.vim/plugin

cp setcolors.vim ~/.vim/plugin

Restart vim or you can source the file from Vim for immediate effect, like done below.

:so ~/.vim/plugin/setcolors.vim 

Now press F8 to try next scheme
Shift F8 to try previous scheme ( if any )
Alt F8 to try a random one.

Awesome!

Vim Tips


% —> means the current buffer
# —>alternate buffer
:ls —> Lists currents buffers
:g/pattern/ —> Globally select the pattern from the file and display
q: —> Open command history
<Ctrl>c<Ctrl>c —> Close command history
:tabnew —> filename Opens file in a new tab
gf –> open file under cursor
. –> repeat last executed command
@: –> repeat last executed :command
e . –> browse directory contents
:browse oldfiles –> List previously edited files
:e #<2 –> starts editing file no. 2 in the history list
:map K dwwP –> transpose adjacent words
:make –> executes makefile
:se list –> Displays special characters such as ^I for tabs..
:scriptnames –> Loaded scripts
zf} –> Fold paragraph using motion, zf preceded by a selection makes that region to get folded
zo –> Open a fold
zc –>  Reclose a fold
:sh Opens a temporary shell exiting will return to vim.  :map <F9> :sh<CR> will be fun!

Vim templates

This should be in  ~/.vimrc

Assuming ~/.vim/templates/ contains the templates files for a specific extension.

Mentioning a specific template
:autocmd BufNewFile *.pl 0r ~/.vim/templates/pl.tpl
:autocmd BufNewFile *.sh 0r ~/.vim/templates/sh.tpl
:autocmd BufNewFile *.html 0r ~/.vim/templates/html.tpl

A more general and intelligent way. This avoids having multiple entries for each extension.

Typical of its many abbreviations, vim provides a shorthand for autocmd; au.
:au BufNewFile * silent! 0r  ~/.vim/templates/%:e.tpl