1

I'm trying to have a .vimrc that is portable between machines. Some computers I use have VimPlugin installed, while others don't. On the ones with VimPlug, I'd like to do some setup using call plug#begin and call plug#end. Currently though, I get an error on the computers without VimPlug, and on those, I'd just like to move on to the rest of the .vimrc.

Optimally, I'd want something like if has('Vim-Plug') and just put the relevant code inside of that. It looks like some embed the installation of the manager right within their rc in an if statement, similarly to what I'd like to do:

if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif

But this doesn't exactly work since vim still interprets the contents of the if statement, even if it doesn't execute it. I get an error: E117: Unknown function: plug#begin even though that code is inside the if statement.

While researching this, I found tons of posts about checking if a particular plugin is installed, but none to check if the manager was installed. Is this not something I should be doing? Does anyone know a way to accomplish this? I'd rather not have to install Vim-Plug on every computer I use the rc on in order to keep it portable.

2 Answers 2

1

If you know a file is precisely installed in a specific place, you can use filereadable().

if filereadable($HOME.'/.vim/autoload/plug.vim')
   call plug#Begin(.....

However, plugins should not be tested to an exact path but to any path in the 'rtp'. This way if some day you migrate to nvim, or to Windows, it'll still work. The right function to do so is globpath().

if ! empty(globpath(&rtp, 'autoload/plug.vim'))
   call plug#Begin(.....
2
  • Awesome, good to know. I’ll check this out when I return to my computer tonight. Thanks!
    – drcomputer
    May 26, 2022 at 15:12
  • This ended up solving the issue. I like the &rtp suggestion. Not planning on switching to nvim anytime soon and definitely not Windows but the flexibility is good, especially to share with others.
    – drcomputer
    Jun 2, 2022 at 21:20
1

You are almost there! The problem here is, that you need to re-source your config because the change in ~/.vim/autoload/plug.vim won't be recognised by vim since it's loading the files in the runtime only once during the startup. So all you need to do is adding source ~/.config/vim/init.vim (if that's the path to your vim config file) to your if-branch:

if empty(glob('~/.vim/autoload/plug.vim')) 
    silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim source
    ~/.config/vim/init.vim " this needs to be added
endif

Edit

If you just want to check if your plugin manager exists, to load your config, then I'd do it as follows:

~/.config/vim/plugins.vim

call plug#begin("<path>")
Plug "bla bla"
" ...
call plag#end()

and then I'd add the following to your ~/.config/vim/init.vim a check, which sources this file, if you have the plugin manager installed:

if !empty(glob('~/.vim/autoload/plug.vim'))
    source ~/.config/vim/plugins.vim " start sourcing your plugins if the package-manager exists
endif
4
  • Hi, thank you for your answer. So I'm not trying to change my config, I only provided the codeblock used in your answer as an example of what I saw in the docs on installing the plugin if it doesn't exist. Rather than installing it if it doesn't exist though, I'd just like to ignore all the code in the if statement. As of now, I'm getting the error about Unknown function: plug because it's trying to interpret code I don't care about. On computers using this vimrc that don't already have Vim-Plug installed, I just want to skip over the if statement and not have it error out on the contents.
    – drcomputer
    May 23, 2022 at 1:15
  • What I'm trying to do inside the if statement is enable my list of plugins. I'd like a portable vimrc that can run without errors, just skipping over the plugins, on machines without the plugin manager, but enable the appropriate plugins on machines that do have the plugin manager.
    – drcomputer
    May 23, 2022 at 1:17
  • @drcomputer I edited my answer, I hope that this helps now
    – TornaxO7
    May 23, 2022 at 14:45
  • 1
    Thanks! I’ll check this when I get back to my computer tonight
    – drcomputer
    May 26, 2022 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.