Is there a way to call arbitrary vimscript functions when the first keypress is made by the user?

I am looking to lazily start-up plug-in when the user starts using vim. This is to save resources when user may start up lots of vims and then not interact with it, for example when using tmux-resurrect.

  • As far as I understand, you want all of your plugins not to be "on", for lack of a better word, just after starting vim. Rather you want to be able to switch them "on" yourself. I think vim-plug can do that for you.
    – 3N4N
    Commented Jun 3, 2018 at 7:06

1 Answer 1


Okay, so the specific ask is how to avoid loading plugins if Vim is launched but not interacted with by a user or, IOW, how to do lazy-loading of plugins, triggered by user activity.

The most obvious way that comes to mind requires that you move all of the applicable plugin directories to pack/plugins/opt under ~/.vim (or wherever you keep your personal vim configuration). Vim does not automatically load plugins unless they are in pack/*/start. It will load plugins on-demand if they are located in pack/*/opt. This is done with the packadd command.

So move the plugins and then add something like this autocommand to your vimrc...

autocmd CursorMoved,CursorMovedI * \
    packadd plugin-name-1 | \
    packadd plugin-name-2 | \
    packadd plugin-name-n

...meaning you have a packadd line for each plugin that was moved. Specifically, replace plugin-name-# with an actual plugin directory name (e.g. vim-fugitive).

It should be pretty clear what this does. As soon as one of the specified events occurs (i.e. the user moves the cursor) each listed plugin will be loaded. You might need to play around with the actual event or events you use...the two CursorMoved events seem like a good start.

There are some refinements that you'll probably want to do if this involves a large number of plugins. For instance, put the packadd calls in a function and call the function from the auto command. Then you can guard the packadd calls with a flag and prevent them from being executed more than once. (Something to consider if there's a noticeable performance hit from running the group of packadd commands repeatedly.)

See :h packadd, :h autocommand, :h autocommand-events for some of the major pieces here.

  • "I'm going to assume you are asking specifically about preventing the loading of plugins rather than a general question about deferring execution of arbitrary vimscript functions. They are mostly different things...if you need help with the other one as well you should open a separate question" in case that it does turn out that OP was concerned with just lazy plugin loading and not the general question, please also suggest an edit to the question to reflect that Commented Oct 2, 2018 at 19:11
  • @PeeyushKushwaha In retrospect, it seems pretty clear to me that OP is asking specifically about lazy-loading of plugins. I removed the opening expository paragraph (which wasn't even in my original post).
    – B Layer
    Commented Oct 2, 2018 at 23:53

Your Answer

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

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