Ohmyzsh: Plugins: can I load them asynchronously, or on-demand?

Created on 6 Sep 2017  ·  3Comments  ·  Source: ohmyzsh/ohmyzsh

oh-my-zsh plugins like pyenv or rbenv are great, but many times I am forced not to use a plugin because it can bring my shell launch time to a crawl. It's why we get issues like #6017 .

Is it possible to load some plugins asynchronously, so my shell prompt is not blocked until all the plugins launch? Failing this, can I load plugins on-demand?

init Feature

Most helpful comment

I can't answer the part about asynchronously loading, but for manual loading you could add a function to your .zshrc that does that.

A basic example (only able to handle one plugin, no error checking)

function load-plugin() {
  source $ZSH/plugins/"$1"/"$1".plugin.zsh
}

Then run load-plugin pyenv, whenever you need it.

All 3 comments

I can't answer the part about asynchronously loading, but for manual loading you could add a function to your .zshrc that does that.

A basic example (only able to handle one plugin, no error checking)

function load-plugin() {
  source $ZSH/plugins/"$1"/"$1".plugin.zsh
}

Then run load-plugin pyenv, whenever you need it.

Just wanted to chime in that there's a very similar issue going on with NVM (Node.js version manager) right now: https://github.com/creationix/nvm/issues/1277.

@crenwick came up with a great way to load NVM on-demand:

alias load_nvm='export NVM_DIR="$HOME/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"'
alias node='unalias node npm && load_nvm && node'
alias npm='unalias node npm && load_nvm && npm'

I'm hoping the same could be done with pyenv:

alias load_pyenv='eval "$(pyenv init -)"'
alias pyenv='unalias pyenv && load_pyenv && pyenv'

@awinecki thanks for that info.

It makes me wonder whether we can have another shell variable lazy_plugins which can be used to either automatically define aliases like the above, or perhaps use zsh autoload so the cost of actually loading the plugin is incurred when the user attempts to use the plugin, rather than at shell startup time.

Would love to hear what the scripting experts think about this idea.

Was this page helpful?
0 / 5 - 0 ratings