简体   繁体   English

在 ftplugin 中声明的 Vim 插件不起作用

[英]Vim plugins declared in ftplugin do not work

Gvim is behaving weird and I can't find the reason. Gvim 表现得很奇怪,我找不到原因。 I use Vundle, and all the plugins declared in my .vimrc are working fine.我使用 Vundle,在我的.vimrc中声明的所有插件都运行良好。 I declared some additional settings and plugins in .vim/after/ftplugin/java.vim .我在.vim/after/ftplugin/java.vim声明了一些额外的设置和插件。

The mappings work fine, but the plugins do not work.映射工作正常,但插件不起作用。 If I choose a different file in my current gvim session, I get those error messages:如果我在当前的 gvim 会话中选择不同的文件,我会收到以下错误消息:

Error detected while processing function vundle#config#bundle[2]..<SNR>14_check_bundle_name:
line 2:
Vundle error: Name collision for Plugin Raimondi/delimitMate. Plugin Raimondi/delimitMate previously used the name "delimitMate". Skipping Plugin Raimondi/delimitMate.
Vundle error: Name collision for Plugin artur-shaik/vim-javacomplete2...
[comment: same error message for all plugins declared in the ftplugin]

I noticed, that if I run :VundleInstall the plugins are suddenly working (the error messages stay when I change the file, no plugins are installed when I use the command).我注意到,如果我运行:VundleInstall插件会突然工作(当我更改文件时错误消息仍然存在,当我使用命令时没有安装插件)。

Here is the beginning of my .vimrc :这是我的.vimrc的开头:

syntax on
set guifont=Inconsolata\ Medium\ 12
set nocompatible
set t_Co=256

filetype off

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

"[comment: all plugins I use for every filetype]

call vundle#end()            " required

filetype plugin indent on

and this is my java.vim file:这是我的java.vim文件:

filetype off
"to automatically close brackets
Plugin 'Raimondi/delimitMate'

"omni-complete for Java
Plugin 'artur-shaik/vim-javacomplete2'

"use tab to navigate through insert completion
Plugin 'ervandew/supertab'
filetype plugin indent on

"needed to make javacomplete2 working properly
autocmd FileType java setlocal omnifunc=javacomplete#Complete

My OS is Ubuntu 16.04.我的操作系统是 Ubuntu 16.04。

You're mistaken regarding what ftplugins are doing and what they should contain.您误会了 ftplugins 正在做什么以及它们应该包含什么。

Ftplugins are loaded once per buffer, every time a new buffer is created/opened.每次创建/打开新缓冲区时,每个缓冲区都会加载一次 Ftplugins。

They are meant to contain buffer local definitions:它们旨在包含缓冲区本地定义:

  • :map <buffer> keybinding action
  • :iab <buffer> keybinding expanded sequence
  • :setlocal option[=value]
  • :command -b CommandName :Action
  • :let b:option = value
  • set the localleader (but be certain it's done before any other ftplugin for the same filetype)设置 localleader(但要确保它在相同文件类型的任何其他 ftplugin 之前完成) (EDIT: localleader is actually a global setting, my mistake) (编辑:localleader 实际上是一个全局设置,我的错误)

They could then load other things that work the same way with :runtime or :so .然后他们可以加载其他与:runtime:so以相同方式工作的东西。 They could contain functions, but it's best to define them into autoload plugins since Vim7.它们可以包含函数,但最好将它们定义为自 Vim7 以来的自动加载插件。 They may contain buffer local menu definitions, but this requires a plugin as this is not standard.它们可能包含缓冲区本地菜单定义,但这需要一个插件,因为这不是标准的。

They are definitively not meant to contain global definition like the ones you have defined.它们绝对不意味着像您定义的那样包含全局定义。 It's not really the place to load global plugins that'll stay activated afterwards.它不是真正加载全局插件的地方,这些插件随后会保持激活状态。

I know that some plugins manager load plugins on the fly depending on the type of the file we work on.我知道一些插件管理器会根据我们处理的文件类型动态加载插件。 I've never shared this need when we are using properly defined ftplugins, and lightweight plugins that only define a few mappings and keep their functions into autoload plugins.当我们使用正确定义的 ftplugins 和仅定义一些映射并将其功能保留在自动加载插件中的轻量级插件时,我从未分享过这种需求。

Last thing, ftplugins are supposed to contain anti reinclusion guards.最后一件事,ftplugins 应该包含反重新包含保护。 On a typical scenario this is not that useful.在典型的情况下,这不是那么有用。 Many use b:did_ftplugin for that purpose, but I avoid this variable as I prefer to have as many ftplugins (for a same filetype) as themes (one that specializes the brackets pairs, one that defines the mapping to automatically expand a switch statement from the type of a variable, one that defines abbreviations for control statements, and so on).许多人为此目的使用b:did_ftplugin ,但我避免使用这个变量,因为我更喜欢拥有与主题一样多的 ftplugins(对于相同的文件类型)(一个专门用于括号对, 一个定义映射以从变量的类型定义控制语句缩写的变量,等等)。 As a consequence I cannot use the same guard for all files.因此,我不能对所有文件使用相同的保护。

All your :Plugin commands are supposed to be between these two lines:你所有的:Plugin命令都应该这两行之间

call vundle#begin()

" :Plugin commands go here

call vundle#end() 

Try another plugin manager if you absolutely need lazy loading.如果您绝对需要延迟加载,请尝试使用另一个插件管理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM