AndrewRadev/switch.vim

plugin works if opening a file from command line, doesn't work if opening a file from vim

Closed this issue · 3 comments

Hi Andrew,

First of all, many thanks for your effort building and maintaining this project.

I've played a bit a few months ago and just came back to it and realized that is not working if I open a file from vim.

  1. I start vim in my terminal and open a file from vim using :e /path/to/file
  • this case Switch plugin won't work - eg. not changing 'foo' to 'bar', seemingly has no effect at all
  1. I start vim in my terminal passing /path/to/file as an argument vim /path/to/file
  • Switch plugin works as intended

I tried with this minimal 4 lines .vimrc

     call plug#begin('~/.vim/plugged')
        Plug 'AndrewRadev/switch.vim'
     call plug#end()
       let b:switch_custom_definitions = [ ['foo', 'bar', 'baz'] ]

Thanks,
Szilveszter

vim: 8.0.494 compiled from source using ./configure --enable-python3interp=dynamic --enable-cscope --enable-rubyinterp --without-x --disable-gui && make && sudo make install
Switch: latest version ddc0697

I've found if I am using
let g:switch_custom_definitions
instead of
let b:switch_custom_definitions in my .vimrc
then Switch works fine even if I open the file from vim.

I think I understand the problem. The custom definitions you've created with b:switch_custom_definitions are buffer-local. That's what the b: prefix means. You can find more information on the topic with :help b:.

Basically, if you wanted to only define switches for a particular filetype, you would use the b: variant. I guess I hadn't explained it well enough in the documentation, so I added some more information about it:

switch.vim/doc/switch.txt

Lines 162 to 170 in f982683

Use the `g:` version of the variable for global definitions, and the `b:`
version for specific filetypes. For instance, here is how you would define
switches only for ruby:
>
autocmd FileType ruby let b:switch_custom_definitions =
\ [
\ ...
\ ]
<

Does this solve your issue?

Yes, that is perfect. Thanks a lot!