Vim 在 ftplugin 中声明的插件不工作

Vim plugins declared in ftplugin do not work

Gvim 运行异常,我找不到原因。我使用 Vundle,在我的 .vimrc 中声明的所有插件都工作正常。我在 .vim/after/ftplugin/java.vim.

中声明了一些额外的设置和插件

映射工作正常,但插件不工作。如果我在当前 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]

我注意到,如果我 运行 :VundleInstall 插件突然工作(当我更改文件时错误消息仍然存在,当我使用命令时没有安装插件)。

这是我的 .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

这是我的 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

我的 OS 是 Ubuntu 16.04.

您对 ftplugins 的作用以及它们应该包含的内容有误。

每个缓冲区加载一次 Ftplugins,每次新缓冲区 created/opened。

它们旨在包含缓冲区局部定义:

  • :map <buffer> keybinding action
  • :iab <buffer> keybinding expanded sequence
  • :setlocal option[=value]
  • :command -b CommandName :Action
  • :let b:option = value
  • 设置 localleader(但要确定它是在相同文件类型的任何其他 ftplugin 之前完成的)(编辑:localleader 实际上是一个全局设置,我的错误)

然后他们可以加载其他与 :runtime:so 工作方式相同的东西。它们可以包含函数,但最好将它们定义为自 Vim7 以来的自动加载插件。它们可能包含缓冲区本地菜单定义,但这需要 plugin 因为这不是标准的。

它们绝对不像您所定义的那样包含全局定义。它并不是真正加载全局插件的地方,这些插件将在之后保持激活状态。

我知道一些插件管理器会根据我们处理的文件类型动态加载插件。当我们使用正确定义的 ftplugins 和仅定义一些映射并将其功能保留在自动加载插件中的轻量级插件时,我从未分享过这种需求。

最后一件事,ftplugins 应该包含反重新包含保护程序。在典型情况下,这不是很有用。许多人为此目的使用 b:did_ftplugin,但我避免使用此变量,因为我更喜欢拥有与主题一样多的 ftplugins(对于相同的文件类型)(一个专门用于括号对,one that defines the mapping to automatically expand a switch statement from the type of a variable, one that 定义控制语句的缩写, 等等)。因此我不能对所有文件使用相同的保护。

你所有的:Plugin命令都应该这两行之间:

call vundle#begin()

" :Plugin commands go here

call vundle#end() 

如果绝对需要延迟加载,请尝试 another plugin manager