Without Pathogen, your single-file script should be placed here:
~/.vim/plugin/stab.vim
With Pathogen or Vundle, you should install it like this:
~/.vim/bundle/stab/plugin/stab.vim
Because of how Vim loads plugins, the Stab
command won't be available when your ~/.vimrc
is executed, however, because of its interactive nature, I'm not sure it is a good idea to execute the Stab
command every time you start Vim.
It is possible, though, to modify it so that it can be run non-interactively, using global variables, for example, or autoloading… But at that point, it would work exactly like having the relevant options in your ~/.vimrc
which doesn't sound that practical to me. And relatively wasteful.
Anyway, the following (quick) version makes it possible to run the script non-interactively from your ~/.vimrc
. You can still use :Stab
if you want to execute it interactively.
For this to work, you must install the script here:
~/.vim/autoload/stab.vim
and add this line to your ~/.vimrc
:
call stab#stab(4, 1)
The first argument is your desired shiftwidth
and the second argument is a boolean: 0
if you don"t want expandtab
, 1
if you want expandtab
. Make sure this line comes after any indentation settings.
Modified script:
if exists("g:loaded_stab") || &cp
finish
endif
let g:loaded_stab = 1
command! -nargs=* Stab call stab#stab(<f-args>)
function! stab#stab(...)
if a:0 > 0
let l:tabstop = 1 * a:1
else
let l:tabstop = 1 * input('set shiftwidth=')
endif
if l:tabstop > 0
" do we want expandtab as well?
if a:0 > 1
let l:expandtab = a:2
else
let l:expandtab = confirm('set expandtab?', "&Yes\n&No\n&Cancel")
endif
if l:expandtab == 3
" abort?
return
endif
let &l:sts = l:tabstop
let &l:ts = l:tabstop
let &l:sw = l:tabstop
if l:expandtab == 1
setlocal expandtab
else
setlocal noexpandtab
endif
endif
" show the selected options
try
echohl ModeMsg
echon 'set tabstop='
echohl Question
echon &l:ts
echohl ModeMsg
echon ' shiftwidth='
echohl Question
echon &l:sw
echohl ModeMsg
echon ' sts='
echohl Question
echon &l:sts . ' ' . (&l:et ? ' ' : 'no')
echohl ModeMsg
echon 'expandtab'
finally
echohl None
endtry
endfunction