It would be neat if I could automatically install all plugins that are not installed when I source $MYVIMRC. So in $MYVIMRC I call plug#installed which checks if all plugins are installed and if not, I execute PlugInstall with the VimEnter event and then install them.
Same with cleaning. It'd be a nice touch.
vim-plug tries to keep its feature set as small and simple as possible. But since it exposes its internal data structure as g:plugs variable, you can use it to implement extra features you need. For auto-installation, add the following code after plug#end() call. It checks the number of plugins whose directories are not found.
if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
autocmd VimEnter * PlugInstall
endif
Likewise, plug#check_clean can be implemented like so:
if len(filter(split(globpath(g:plug_home, '*'), "\n"), 'isdirectory(v:val)'))
\ > len(filter(values(g:plugs), 'stridx(v:val.dir, g:plug_home) == 0'))
autocmd VimEnter * PlugClean
endif
though I'm not sure if you would really want to pay the cost of checking the directories every time you start your Vim.
You might want to check out the following wiki pages to get some ideas on what can be done.
Could I also execute something when vim-plug is done installing everything? I want to source $MYVIMRC automatically.
Hmm, your question made me realize an important issue. The installer of vim-plug is asynchronous on Vim 8 and Neovim, but it works synchronously during has('vim_starting') to allow use cases like vim +PlugUpdate +qa. However, on VimEnter, has('vim_starting') is not true, so the suggestion I made above autocmd VimEnter * PlugInstall | q is no longer valid on Vim 8 or on Neovim.
For that reason, I just added --sync flag to PlugInstall and PlugUpdate commands. PlugUpgrade and try this.
if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC | q
endif
thanks!
There were a few problems with your examples. I fixed it all up here https://github.com/nhooyr/dotfiles/blob/fc64cf3c551d61c608ddee7e2b1b68e2b9a09074/.config/nvim/init.vim#L167-L186
The check for :PlugClean! works now even if some plugin is listed but not installed and :PlugInstall only installs what is not currently installed.
Most helpful comment
There were a few problems with your examples. I fixed it all up here https://github.com/nhooyr/dotfiles/blob/fc64cf3c551d61c608ddee7e2b1b68e2b9a09074/.config/nvim/init.vim#L167-L186
The check for
:PlugClean!works now even if some plugin is listed but not installed and:PlugInstallonly installs what is not currently installed.