VimL: Checking if function exists

别说谁变了你拦得住时间么 提交于 2019-12-03 10:51:12

Use

if exists("*GitBranchInfoString")
    " do stuff here
endif

The currently selected answer doesn't work for me (using Vim 7.4 / Ubuntu). I believe that's because:

.vimrc is sourced before any plugins are loaded

As @ZyX noted this in a comment.

My preferred method is just to check for the existence of the plugin file. I find this cleaner than writing a separate function in an external file.

if !empty(glob("path/to/plugin.vim"))
   echo "File exists."
endif

Just as an alternative you may also use a regexp to decide if the plugin at hand is in your runtimepath:

if &rtp =~ 'plugin-name'
    ...
endif

This has the advantage that it works with plugins that only have vimscript code in the autoload directory, which in turn can't be detected when .vimrc is initially parsed since the autoload snippets are loaded at the time of a function call.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!