Tuesday, April 3, 2012

Simple environment toggle plugin for vim

I use vim a lot for work.  I use it for a number of different things.  I've found that in certain circumstances, I need some settings in place and in others situations, I need different settings.  For example, when I'm editing a python script, I need some specific settings in order to keep pylint happy.

Below is a file that I've dropped in my ~/.vim/plugins directory.  I named it pyvi.vim.  It let's me toggle between "normal" settings and "python" settings:

function! PyVi()
if exists('g:pyvi_on')
unlet g:pyvi_on
set noexpandtab
set textwidth=78
set tabstop=8
set softtabstop=0
set shiftwidth=8
set noautoindent
unmap <Leader>r
echo "Pyvi off."
else
let g:pyvi_on = 1
set expandtab
set textwidth=79
set tabstop=8
set softtabstop=4
set shiftwidth=4
set autoindent
map <Leader>r :!nosetests<cr>
echo "Pyvi on."
endif
endfunction
command Pyvi call PyVi()
Now, when I'm in vim, if I issue the command "Pyvi" once, it set's a bunch of settings for me that are specific to making pylint happy.  If i issue the same command again, it will undo my settings changes.  I also added the following to my .vimrc to create a mapping for \-d.
map <Leader>d :Pyvi<cr>
Pretty simple and yet effective.