Vim startup time with "opam list" can be long

It’s been like an itch I subconsciously scratch but never fully enters my thinking: Vim startup was too slow.

It might be that I don’t run as many plugins as others, or I might have something else amiss (or old vim-ocaml integration)… but it was pretty simple to find that the call to “opam list” which is added to .vimrc made the difference from 0.1s startup to 0.5s startup on my wimpy notebook.

This is used to verify which of ocp-indent, ocp-index, and merlin are installed, which is a reasonable precaution. But in my case, I’ve changed this to a static list (which will break if/when I don’t have them installed).

Usually I have a long-running Vim session for code, so startup time is hardly noticed.

The problem is that this opam check happens for any vim startup – like, editing a quick note or a config file, or any time my $EDITOR/VI are called for by another program.

Anyway, I’m noting this more for anyone else who might feel this “itch”, in case it wasn’t quite realized, as for me. I’m not suggesting this aspect of vim integration should be changed – unless someone has a clear improvement!


I just ran into this as well, perhaps I haven’t thought about it enough, but it also seems to break down with various opam switch workflows. Which might highlight why the current setup should be reconsidered.

I’m not sure what you’re referring to, is it opam that adds opam list to your .vimrc now ?

In any case, it seems a bad idea indeed, provided how slow opam list is. Maybe you can avoid it but still test whether merlin and friends exist, like so:

let g:opamshare=substitute(system('opam config var share'),'\n$','','')
if isdirectory(g:opamshare . '/ocp-indent')
  " TODO: do stuff when ocp-indent exists, e.g.:
  execute 'set rtp^=' . g:opamshare . '/ocp-indent/vim'
if isdirectory(g:opamshare . '/merlin')
  " TODO: do stuff when merlin exists, e.g.:
  execute 'set rtp^=' . g:opamshare . '/merlin/vim'
" etc.

Or, if opam list is really needed, you may at least make it run lazily, only when you first load an OCaml file:

au FileType ocaml :call LoadOCamlPluginsOnce()
fun! LoadOCamlPluginsOnce()
  if exists('g:ocaml_loaded_once') && g:ocaml_loaded_once = 1
  let g:ocaml_loaded_once = 1
  " TODO: do the stuff with `opam list`

Those both seem like better ideas, more in-tune with how vimscripts tend to go! I might use the directory hack rather than my (self-)lazy and fragile hack. :slight_smile:

While the autoload on filetype would be an improvement with no downside(?) to the current opam user-setup, I think.

I think expecting plugins to be in the current Opam switch is a bad idea. You’ll get inconsistent behavior depending on which Opam switch is active when Vim is started and have weird setup code in your .vimrc.

vim-ocaml has the command :Opam <switch name> to change the current Opam switch, which won’t with that setup.

I suggest installing these plugins like regular Vim plugins. For example, by copying the last version to ~/.vim/pack/plugins/start.