Skip to content

Instantly share code, notes, and snippets.

@dblanken
Last active August 12, 2021 09:57
Show Gist options
  • Save dblanken/716cc0aa894de11e8a2932119878bb26 to your computer and use it in GitHub Desktop.
Save dblanken/716cc0aa894de11e8a2932119878bb26 to your computer and use it in GitHub Desktop.
Test vim plugin existence

How to test the existence of a plugin for your vimrc

I wanted to get this down since I find myself playing with plugins, but would like to not have to delete configuration of it in the event I need it again. There are also plugins that interact with eachother and I'd like to gracefully degrade if a plugin is not there. This is one option, to check the runtimepath of the system after the vimrc has ran.

From what I can tell, the only way to do this inside the vimrc would be to traverse all of the pack directories for the existence of the plugin, which seems duplicative of what vim already does.

The following should not be used in the vimrc, since the runtimepath is not yet set till after that happens. Any plugin configuration which depends on a check must be at least in the plugins folder, if not in the after folder somewhere.

Note: This is for plugins that use the packages feature of vim and are auto loaded in the start folder or you have manually packadd! them in your vimrc. This will have no idea a plugin is in the opt directory since it's not added to the runtimepath until 'packadd'ed.

Usage:

  if (PluginExists('vim-rails'))
    " do stuff
  endif
function! PluginExists(plugin) abort
  return stridx(&rtp, a:plugin) > -1
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment