Saturday, March 26, 2016

Re: Packages: best practices?

On 2016-03-25 19:15:59 +0000, Nicola said:


I would like to start using Vim's new package feature, but first I want to make

sure that I have understood how it is supposed to be used. Is it too early to

ask for tips about that?


First let me summarize what I have understood about packages (please correct

anything wrong):


- every package must be inside a directory called `pack` (`~/.vim/pack`).


- Each package should have (at least one of) two subdirectories: `start` and

  `opt`.


- Plugins and ftplugins that must be available at launch time must go inside

  `start`. What actually happens is that *any* directory under `start` is added

  to `runtimepath` at startup independent of its content (much like Pathogen

  does). Besides, if a directory contains a `plugin/foo.vim` file, that file is

  sourced.


- Plugins and ftplugins that you want to be loaded lazily/manually go inside

  `opt`. These may be loaded with `:packadd`. This is not exactly like loading

  a plugin at startup, though, because if a plugin does something on VimEnter,

  that something must be triggered manually.


- The `colorscheme` command searches both `pack/*/start` and `pack/*/opt/`

  (beside `runtimepath`), so it does not matter where a colorscheme is.


If the above is accurate, I would give the following recommendations:


- Always put filetype plugins (no `plugin/foo.vim`) inside `start`. I see no

  reason why you would want to do otherwise (unless you have a filetype that you

  seldom use and you really want to keep `runtimepath` as small as possible).

  This type of plugin does not cause any file to be loaded at startup, unless

  there is an `ftdetect/xyz.vim` file.


- Always put colorschemes inside `opt`. This avoid cluttering `runtimepath`, and

  they are found anyway.


I have implemented the schema above, using the following directory structure:


pack/

  bundle/

    start/

    opt/

  themes/

    opt/


All seems fine. For colorschemes things are not as simple as I have described

above, because they may contain autoload directories, with (typically) stuff for

Airline/Lightline or for GUI Vim. Since I don't use those, for me it's fine to

have colorschemes inside `opt`, but in general some colorschemes would better go

inside `start`.


Plugins that I had blacklisted in Pathogen are now in bundle/opt. Pathogen lives

in bundle/opt, too: I still use it as a fallback when packages are not available.


A positive note is that the startup time has improved ~25% (now it's ~60ms).


Then, everything should work as before, with the exception of YCM, which must

now be loaded manually. To do so, I need


:packadd youcompleteme


but also


:call youcompleteme#Enable()


because :packadd does not trigger VimEnter.


This is a bit a of a pain. How about having `:packadd` trigger an event that

plugins may hook to?


Nicola


No comments: