4

I have my dotfiles synchronized on github which is a current practice.

As I use several machines I want to be able to have some part of my configuration not synched on my github repo. To do so I added these lines in my synched .vimrc:

let $MYLOCALVIMRC = $HOME . "/.local.vim"
if filereadable($MYLOCALVIMRC)
    source $MYLOCALVIMRC
endif

Which allows me to add some configuration to ~/.local.vim which isn't synched.

This works well but now I'd like to extend that to the plugins: I'd like to have some plugins only loaded on some machines.

I'm using vim-plug as my plugin manager and it is not possible to call plug#begin('~/.vim/plugged') and plug#end() several times. Even when changing the directory of plug#begin().

The only solution I could imagine was to use a second plugin manager for the local vimrc but it is pretty ugly and I think that it might create some conflicts on the runtime path.

TL;DR Is it possible, using a plugin manager, to load some plugins in a .vimrc file and some other plugins in a local configuration file sourced by the first .vimrc?

  • Why not just build up a list of plugins to load, and then load them in a for loop at the end of .vimrc? Surely your plugin manager supports loading a function via a function call? – jpaugh May 7 '16 at 1:32
  • @jpaugh because that would mean modifying my vimrc on each of the different machines, which would imply some additional operations when pushing/pulling my dotfiles from my git repo. I agree that they are minor operations but with this method it is totally transparent. – statox May 7 '16 at 10:15
  • Wait, I think I'm missing something. I thought you wanted to have a separate .local.vim for each machine? You could add to the plugins list in the local file, then load them all in the main .vimrc. Creating the plugin list, and loading all plugins would happen in the .vimrc, but running the local script in between would give it the chance to modify the list of plugins. – jpaugh May 7 '16 at 15:12
  • I'm not sure I understand what you mean: in your first comment you suggested adding lines to my vimrc which is not what I want. Now you suggest adding lines to a local.vimrc which is indeed what I want and what the solution of VanLaser says. I also don't understand what you mean with the for loop (I know what a for loop is but I don't understand what you want to do with it) – statox May 7 '16 at 18:39
  • Yes. If you set up an (empty) list of plugins to load in your .vimrc file, the call the machine-local script, then that script can add whatever it want to that list. Each local script could add something different, and the .vimrc would treat it the same way in each case. Finally, the main .vimrc would load the plugins mentioned in the list. Does that make sense? – jpaugh May 8 '16 at 5:05
2

(I haven't tested this)

You could use another local file, that contains exclusively Plug ... commands. The main vimrc would load this, inside the plug#begin - plug#end section. Something like:

let $MYLOCALPLUGINS = $HOME . "/.local.plugins.vim"

plug#begin('~/.vim/plugged')
Plug '...'

" load local plugins
if filereadable($MYLOCALPLUGINS)
    source $MYLOCALPLUGINS
endif

Plug '...'
plug#end()

let $MYLOCALVIMRC = $HOME . "/.local.vim"
if filereadable($MYLOCALVIMRC)
    source $MYLOCALVIMRC
endif

Alternatively (and simpler), just move the $MYLOCALVIMRC creation above plug#begin, and load the extra (locally-used) plugins directly in the main vimrc, on condition that this local vimrc exists:

let $MYLOCALVIMRC = $HOME . "/.local.vim"

plug#begin('~/.vim/plugged')
Plug '...'

" load local plugins HERE
if filereadable($MYLOCALVIMRC)
    Plug '...'
    Plug '...'
endif

Plug '...'
plug#end()

if filereadable($MYLOCALVIMRC)
    source $MYLOCALVIMRC
endif
  • Sourcing another file between plug#begin() and plug#end() is pretty simple but on first tests it seems like its working, thanks! Also I'm not sure I understand what you exactly mean on your alternative solution. – statox Sep 12 '15 at 23:30
  • You're welcome :) Re: alternative, I added an example to the answer. – VanLaser Sep 12 '15 at 23:38
  • Oh I get your example now but the interesting point with the local file is that my "local" plugins are not synched on github, if I write them directly in the versionned .vimrc I'll find them on my other machines (even if they are not sourced). – statox Sep 12 '15 at 23:41
  • If the plugins are "truly" local (e.g. you are making your own plugins, experimental etc.), you don't need Plug (while you can use it) - simply add your path to the runtimepath in your local vimrc. – VanLaser Sep 14 '15 at 9:43
  • 1
    You're right for this point. But I didn't expressed myself clearly: I meant if I create a if in my vimrc which checks if a file exists to install some plugins , the content of this if (i.e. The plugins I want only on this machine, i.e. The lines Plug '. ..' ) will still appear in my vimrc which isn't convenient. Anyway I've been testing your first solution for some days and it is exactly what I was looking for so I'm accepting your answer. Thanks again :-) – statox Sep 14 '15 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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