Plugin manager in vimrc

3 Points

Evgeni Kolev Evgeni Kolev

10 years ago

Simple plugin manager in vimrc (see first few lines of the snippet for description; sorry, at time of writing I can't do any formatting in this description)

" I call this snippet: Poor man's plugin downloader
"
" This snippet allows you to define your plugins in vimrc, like so (one plugin per line or many plugins per line):
" Pl 'tpope/vim-repeat'
" Pl 'tpope/vim-sensible'  'tpope/vim-commentary' 'tpope/vim-eunuch'
" 
" Next time you start vim, these plugins will be fetched from github and enabled.
" 
" Existing plugins can be updated with:
" :UpdatePlugins
" 
" Or, if you have vim-dispatch, plugins can be updated in the background with:
" :UpdatePlugins!
" 
" This snippet relies on battle-tested vim-pathogen to do the heavy lifting.
" --------------------------------------------------------------------------
"
" Modify the 'Plugins {{{' section below to specify your plugins
"
" Poor man's plugin downloader {{{
let g:plugin_dir = expand('~/.vim/bundle', ':p')
let g:plugin_hash = {}
let g:pathogen_blacklist = []

if !isdirectory(g:plugin_dir) | call mkdir(g:plugin_dir, "p") | endif

function! DownloadPluginIfMissing(plugin) abort
  let output_dir = g:plugin_dir . '/' . fnamemodify(a:plugin, ":t")
  if isdirectory(output_dir) || !executable('git')
    return
  endif
  let command = printf("git clone -q %s %s", "https://github.com/" . a:plugin . '.git', output_dir)
  echo "DownloadPluginIfMissing: " . command | echo system(command)
  silent! execute 'helptags ' . output_dir . '/doc'
endfunction

function! UpdatePlugin(plugin) abort
  let output_dir = g:plugin_dir . '/' . fnamemodify(a:plugin, ":t")
  if !isdirectory(output_dir) || !executable('git')
    return
  endif
  let command = printf("cd %s && git pull -q", output_dir)
  echo "UpdatePlugin: " . command | echo system(command)
endfunction

function! Pl(...) abort
  for plugin in map(copy(a:000), 'substitute(v:val, ''''''\|"'', "", "g")')
    let g:plugin_hash[ fnamemodify(plugin, ':t') ] = 1
    call DownloadPluginIfMissing(plugin)
  endfor
endfunction

command! -nargs=+ Pl call Pl(<f-args>)
command! -bang -nargs=0 UpdatePlugins if len("<bang>") == 0 | call map( keys(g:plugin_hash), 'UpdatePlugin( v:val )' ) | Helptags | else | execute "Start! vim -c UpdatePlugins -c Helptags -c qa" | endif
" }}}


" Plugins {{{
Pl 'tpope/vim-sensible'  'tpope/vim-commentary' 'tpope/vim-eunuch'
Pl 'tpope/vim-obsession' 'tpope/vim-tbone'      'tpope/vim-unimpaired'
Pl 'tpope/vim-git'       'tpope/vim-markdown'   'tpope/vim-fugitive'
Pl 'tpope/vim-dispatch'  'tpope/vim-rsi'        'tpope/vim-repeat'
Pl 'tpope/vim-jdaddy'    'tpope/vim-surround'   'tpope/vim-projectionist'
" }}}

set nocompatible
syntax on
filetype plugin indent on
Pl 'tpope/vim-pathogen'
execute "source " . g:plugin_dir . '/vim-pathogen/autoload/pathogen.vim'
let g:pathogen_blacklist = filter(map(split(glob(g:plugin_dir . '/*', 1), "\n"),'fnamemodify(v:val,":t")'), '!has_key(g:plugin_hash, v:val)')
execute pathogen#infect(g:plugin_dir . '/{}')
" }}}

Javier Blanco GutiƩrrez

Javier Blanco GutiƩrrez 10 years ago

Nice code!

yang-ling

yang-ling 10 years ago

Try Vundle: https://github.com/gmarik/Vundle.vim

Or NeoBundle: https://github.com/Shougo/neobundle.vim

I use the latter.

valdoonicanlives

valdoonicanlives 9 years ago

Thanks, tried it out and it works fine, using it now :)