28

right now I'm cleaning up my .vimrc file to make sure it's compatible on most systems.

In my statusline I use a function that another plugin sets, the GitBranchInfoString() function introduced by this plugin.

What I wanna do is check if this function is set, and only then add it to the statusline. It would be in it's own line so I just need to check for it.

What would be the simplest way to accomplish this?

Thanks for all your help!

EDIT:

I have the following:

if exists('*GitBranchInfoString')
    let &stl.='%{GitBranchInfoString()}'
endif
greduan
  • 4,770
  • 6
  • 45
  • 73

3 Answers3

41

Use

if exists("*GitBranchInfoString")
    " do stuff here
endif
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • It does check for it, but for some reason it doesn't apply it to the statusline. I updated question with what I have. – greduan Dec 04 '12 at 19:36
  • 6
    @Eduan vimrc is sourced before any plugins are loaded. Use this condition on `VimEnter` event, put statusline stuff into `~/.vim/after/plugin/statusline.vim` (`statusline` can be any name) or do `runtime plugin/git-branch-info.vim` prior to the check (it will forbid you to disable this plugin with `--noplugin` option though; other solutions won’t). – ZyX Dec 04 '12 at 19:46
  • I see, I will try that later, and give you any feedback. :) – greduan Dec 04 '12 at 19:51
  • For a more complete answer: http://superuser.com/questions/552323/how-can-i-test-for-plugins-and-only-include-them-if-they-exist-in-vimrc – user3751385 May 25 '15 at 18:33
8

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.

bergercookie
  • 2,542
  • 1
  • 30
  • 38
7

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
user3751385
  • 3,752
  • 2
  • 24
  • 24
  • 1
    +1. Sample for checking if Syntastic is installed via Vundle: `if !empty(glob(expand(":p:h") . "/.vim/bundle/syntastic"))`. (See [here](http://superuser.com/a/120011/4160).) – Josh Kelley Feb 21 '17 at 04:07