Skip to content

Instantly share code, notes, and snippets.

@bfrg
Last active February 20, 2020 00:28
Show Gist options
  • Save bfrg/36e016da272f8e507f551b115872380d to your computer and use it in GitHub Desktop.
Save bfrg/36e016da272f8e507f551b115872380d to your computer and use it in GitHub Desktop.
Quickly open the root directory of a plugin under ~/.vim/pack using the default file explorer
" ==============================================================================
" Quickly open the root directory of a plugin under ~/.vim/pack using your
" default file explorer (dirvish, netrw, etc.)
"
" Commands:
" Open root directory of plugin <plug> ...
" :PackE {plug} ...in current window
" :PackS {plug} ...in new split
" :PackV {plug} ...in new vertical split
" :PackT {plug} ...in new tab page
"
" Examples:
" :PackE vim-q<tab>
" tab-completes to the following installed plugins (if installed)
" vim-qlist <https://github.com/romainl/vim-qlist>
" vim-qf <https://github.com/romainl/vim-qf>
"
" :PackT vim-qf
" opens the directory of vim-qf, e.g. ~/.vim/pack/git-plugins/start/vim-qf
" in a new tab using the default file browser (dirvish, netrw, etc.)
" ==============================================================================
" Zip two lists into a dictionary
" From: https://stackoverflow.com/a/37442954
function! s:ZipAsDict(l1, l2) abort
if len(a:l1) != len(a:l2)
throw 'Zip operation cannot be performed on lists of different sizes.'
endif
let dict = {}
for i in range(0, len(a:l1)-1)
let dict[a:l1[i]] = a:l2[i]
endfor
return dict
endfunction
" Return a dictionary with the name of the plugin and its full path
" Example:
" {
" 'vim-qf': '~/.vim/pack/plugins/start/vim-qf',
" 'vim-fugitive': '~/.vim/pack/plugins/start/vim-fugitive',
" ...
" }
function! s:Roots() abort
let pack_paths = glob('$HOME/.vim/pack/*/*/*/', 0, 1)
let pack_names = map(copy(pack_paths), {_,item -> fnamemodify(item, ':h:t')})
return s:ZipAsDict(pack_names, pack_paths)
endfunction
function! s:Packages(ArgLead, CmdLine, CursorPos) abort
let plugins = s:Roots()
return filter(keys(plugins), 'v:val =~ a:ArgLead')
endfunction
function! s:OpenPackageDir(arg, cmd) abort
let plugins = s:Roots()
if has_key(plugins, a:arg)
execute a:cmd . ' ' . plugins[a:arg]
else
echohl ErrorMsg
echo 'Unknown plugin! Use tab-completion or <C-d> to display available plugins.'
echohl None
endif
endfunction
command! -nargs=1 -complete=customlist,<sid>Packages PackE call <sid>OpenPackageDir(<q-args>, 'edit')
command! -nargs=1 -complete=customlist,<sid>Packages PackV call <sid>OpenPackageDir(<q-args>, 'vsplit')
command! -nargs=1 -complete=customlist,<sid>Packages PackS call <sid>OpenPackageDir(<q-args>, 'split')
command! -nargs=1 -complete=customlist,<sid>Packages PackT call <sid>OpenPackageDir(<q-args>, 'tabedit')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment