0

for long period I'm looking for a good indentation plugin, and recently I found: https://stackoverflow.com/a/1610732/2034967 this is an awesome indentation(the best in my personal opinion) and I use it permanently - part of my .vimrc file and every time I start vi I turn it on, and if not, then I use :retab to fix the \t tabs after turning on :Stab.

My actual question is how to turn it into a separate bundle bundle and to configure it to auto start with the wanted(to use 4 spaces instead of tab) parameters configurations automatically and only disabling(turning off and going back to default tabs) the plugin on demand using :Stab?

Background about my working environment: vim running on linux (mostly MCP linux distro) in command line (not GUI vim)

I'm asking it because I tried to follow the vim guides to create a plugin and failed.. I event tried some vim plugins for managing plugins like https://github.com/tpope/vim-pathogen and without success.

Thanks for the help!

Community
  • 1
  • 1
Elia
  • 193
  • 1
  • 3
  • 11
  • What vim guide did you follow? How did it fail? – romainl Mar 20 '13 at 06:32
  • http://vim.wikia.com/wiki/How_to_write_a_plugin for the vim plugin and I tried to set up vim-pathogen and define Stab as a bundle. How did it fail? I created a the plugin file from the Stab source lined above in `~.vim/Stab` but couldn't activate it from the vim command line using `:Stab` , same result from the pathogen created a the file under: `~/.vim/bundle` and inserted the pathogen execution into my .vimrc but still couldn't activate `:Stab` – Elia Mar 20 '13 at 07:33

1 Answers1

0

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
romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thanks, this works, when vim loads it loads stab, I have two additional questions following your explanation: 1) 2) – Elia Mar 20 '13 at 11:06
  • Full comment: Thanks, this works, when vim loads it loads stab, I have two additional questions following your explanation: 1) when vi loads it prompts to press "enter" to validate the given values, is there an way to configure the function in a way that it will not wait for the "enter" configraion? 2) is it possible to configure this script so it will be possible toggle back to default settings - that tabs are regular tabs and not spaces spaces? thanks for the help mate! – Elia Mar 20 '13 at 11:12
  • I would answer "yes" to both questions but I don't have much time to dig further ATM. Maybe tonight! – romainl Mar 20 '13 at 13:01