Danwizard208
Danwizard208

Reputation: 195

In vim with pathogen, how can I check if a plugin will be loaded?

I'm using pathogen to manage my plugins in vim, and I have a section in my vimrc that I would like to run only if a certain plugin isn't installed/won't be loaded. E.g.

if(dwm.vim not in use)
  nnoremap <C-h> <C-w>h
  nnoremap <C-j> <C-w>j
  nnoremap <C-k> <C-w>k
  nnoremap <C-l> <C-w>l
endif

The two options that would probably work in most cases are to check if dwm.vim is in g:pathogen_disabled and/or to check if the directory .vim/bundle/dwm.vim exists.

However these checks seem somewhat brittle. For example if I don't have dwm at all checking for dwm.vim not to be pathogen disabled is insufficient, and if for whatever reason dwm.vim is in some nonstandard location that still makes it into the runtimepath the path check won't work.

The whole point of pathogen is to make managing my runtimepath easier, so an elegant solution would be to search this path. But searching the help, the pathogen source, google, and here revealed no easy way to do this. Can I do this in vim/pathogen natively? Is there a plugin that will do this for me? Should I not be doing this at all, since the cases where the checks fail are fairly corner-casey and won't happen as long as I manage my plugins properly?

Upvotes: 0

Views: 1080

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45177

Most plugins have a (re)inclusion guard. Usually named g:loaded_pluginname. Using this and the after directory you can conditionally load your mappings.

Put the following in ~/.vim/after/plugin/dwm.vim:

if !get(g:, 'loaded_dwm', 0)
  nnoremap <C-j> <C-w>j
  nnoremap <C-k> <C-w>k
  nnoremap <C-l> <C-w>l
endif

For more help see:

:h write-plugin
:h after-directory
:h get()

Upvotes: 3

Sato Katsura
Sato Katsura

Reputation: 3086

Add this after pathogen#infect():

if globpath(&runtimepath, 'dwm.vim', 1) !=# ''
    " dwm.vim caught in the act
endif

Upvotes: 2

Related Questions