Search code examples
vimvim-plugin

How is the vim-plug managing my vim plugins?


When I check my .vim directory, I found that all the plugins installed with vim-plug is sitting in the .vim/plugged directory. In this case, how did vim load these plugins? If I have a same plugin normally installed, then which one will have a higher propriety to be loaded?


Solution

  • The plug#begin(...) function sets the "home" path for the plugin (vim-plug is a plugin too),

      if a:0 > 0
        let s:plug_home_org = a:1
        let home = s:path(fnamemodify(expand(a:1), ':p'))
      ...
    

    and the function plug#end() goes through the list of plugins defined (via plug#()), and keeps them in a dictionary:

      for name in g:plugs_order
      ...
             if has_key(plug, 'on')
          let s:triggers[name] = { 'map': [], 'cmd': [] }
          for cmd in s:to_a(plug.on)
            if cmd =~? '^<Plug>.\+'
              if empty(mapcheck(cmd)) && empty(mapcheck(cmd, 'i'))
                call s:assoc(lod.map, cmd, name)
    

    then finally manipulates the runtimepath and sources each one of the plugins by calling (eventually) s:lod():

      for [cmd, names] in items(lod.cmd)
        execute printf(
        \ 'command! -nargs=* -range -bang %s call s:lod_cmd(%s, "<bang>", <line1>, <line2>, <q-args>, %s)',
        \ cmd, string(cmd), string(names))
      endfor
    

    You can figure out the order in which plugins are loaded from vim-plug's code.