1

In an effort to automate addition of plugins in vim based on the filetype I decided to create a simple function in my .vimrc file. I am using Vundle as plugin manager and as required by Vundle, filetype should be switched off. I decided to save filetype in a variable and use it afterwards within the the plugin list inside Vundle. Here is the relevant .vimrc section:

set nocompatible              " be iMproved, required
filetype plugin indent on
let var = &ft
echo var
function Plugin_set(var)
   if (a:var == "python")
     Plugin 'Python-mode-klen'
   else
     Plugin 'scrooloose/syntastic'
   endif
endfunc
"Vundle Section Begin
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" All of your Plugins must be added after the following line
Plugin 'Tagbar'
Plugin 'VundleVim/Vundle.vim'
Plugin 'surround.vim'
Plugin 'better-snipmate-snippet'
Plugin 'bling/vim-airline'
Plugin 'Tabular'
Plugin 'rodjek/vim-puppet'
call Plugin_set(var)
" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

I am getting no output at the line "echo var" where I would expect the filetype to appear. Then of course the if statement evaluates to false and else is used no matter what actual filetype I am opening.

But when I am trying to give the same commands in the vim shell like this:

:let var = &ft
:echo var

I am getting the actual filetype assigned to the var variable.

The if statement also evaluates to true if I just use let var = "python" instead of let var = &ft

It looks to me that I don't understand the rules of assignment of parameter values to variables.

But I just have no clue what is wrong here.

I very much appreciate your help.

1 Answers1

2

The commands in your vimrc are executed only once, before the loading of any buffer, so there's no buffer and thus no &filetype by the time Vundle does its questionnable "magic".


But what you are doing is globally pointless. Filetype-specific plugins are usually designed in such a way that most of their filetype-specific features are loaded only when a buffer with that filetype is loaded. This is a completely automatic mechanism handled via the ftplugin feature.

A filetype-specific plugin that is fully loaded at startup is a poorly written plugin that doesn't deserve to be installed.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thanks! These two plugins that I was trying to enable based on the filetype seem to be mutually exlcusive - if they both are enabled, they start interfering with each other (at least that's what I found out for puppet filetype) so the only solution is to enable only one at a time. Does it mean that the only simple solution would be to edit .vimrc every time before I want to edit python or other files. – Danil Zhigalin Jan 18 '16 at 11:54