简体   繁体   中英

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. Eg

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?

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()

Add this after pathogen#infect() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM