I am trying to load my optional language packages based on filetype. I am using vim8 native package feature.

In vim I can load them with :packadd vim-javascript. However, I read in :help packadd - (emphasis mine)

:pa[ckadd][!] {name}

Search for an optional plugin directory in 'packpath' and source any plugin files found. The directory must match: pack/*/opt/{name}

The directory is added to 'runtimepath' if it wasn't there yet. If the directory pack/*/opt/{name}/after exists it is added at the end of 'runtimepath'.

If loading packages from pack/*/start was skipped, then this directory is searched first: pack/*/start/{name}

Note that {name} is the directory name, not the name of the .vim file. All the files matching the pattern pack/*/opt/{name}/plugin/**/*.vim will be sourced. This allows for using subdirectories below plugin, just like with plugins in 'runtimepath'.

If the filetype detection was not enabled yet (this is usually done with a syntax enable or filetype on command in your .vimrc file), this will also look for {name}/ftdetect/*.vim files.

When the optional ! is added no plugin files or ftdetect scripts are loaded, only the matching directories are added to 'runtimepath'. This is useful in your .vimrc. The plugins will then be loaded during initialization, see |load-plugins|.

Also see |pack-add|. {only available when compiled with +eval}

So I try to write something in my vimrc but it doesn't work.

autocmd! FileType javascript packadd vim-javascript
" Or
autocmd FileType javascript packadd! vim-javascript 

I found this Reddit post but as I mentioned, it doesn't work.


1 Answer 1


how to load vim8 optional packages in vimrc?

The way to load vim8 optional packages in your vimrc is to use the :packadd! command, which will add them to your 'runtimepath', which will make the opt plugins load together with the ones in start.

In your particular case:

packadd! vim-javascript

The examples you give hint that what you're actually trying to do is load a vim8 optional package from an autocmd, which is a slightly different question.

The reason they don't work is most likely because you're already processing a FileType event at that point, and the plugin you're loading will register new autocmds for that same event, but those will most probably not be triggered for that same event you're already handling. Maybe you could hack something together using doautocmd to process those commands again, but I really don't think that would be worth it.

The problem here is that you're trying to optimize something which is not a performance problem, or at least you haven't found it to be one.

Like most well written plug-ins, vim-javascript won't really load much of anything at the time it's loaded and the bulk of it will only get loaded once you start to actually use it (in this case, once you start editing JavaScript sources.)

Since vim-javascript (like most language plug-ins) doesn't carry any plugin/*.vim files, the only code that will get loaded at startup is ftdetect/javascript.vim, which has only 11 lines of Vimscript, should load really quickly and it's something you actually want loaded at startup, since it contains interesting bits of FileType detection (such as looking for a node shebang) that you'd be otherwise missing.

The :help pack-add page has some interesting comments on the rationale for opt and what should be stored there:

Where to put what


Filetype plugins should go under pack/*/start, so that they are always found. Unless you have more than one plugin for a file type and want to select which one to load with :packadd. E.g. depending on the compiler version:

if foo_compiler_version > 34
    packadd foo_new
    packadd foo_old

In other words, the intent of having filetype plug-ins under opt is not to add the plug-in on demand. It is to add the plug-in conditionally, based on presence of some feature in your system which can be dynamically detected from your .vimrc. For instance, you might want to detect whether a node binary exists or if eslint is present or similar, and only enable vim-javascript in those cases.

The problem of loading a plug-in's scripts on demand had been already solved, that's by using autoload and ftplugin. So the cost of adding it unconditionally is quite low, almost insignificant, at Vim startup. Definitely not a reason to try to add it from an autocmd.

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.