143

How do I install a plugin in Vim?

Does it matter:

  • Whether I use vi or Vim?
  • Whether I use gVim?
  • Which version of Vim I'm using?
  • What my OS is?
elyashiv
  • 2,429
  • 2
  • 18
  • 23

7 Answers7

86

To install a plugin, we need to know what form it comes in. It can be:

A single .vim file is supposed to be placed in the .vim/plugin directory.

A Vimball file can be installed by opening it in Vim and running :source %.

A set of files in the standard directory layout can be installed either by copying them to .vim, or using a plugin package manager.

Plugins may depend on certain features. Therefore:

  • It may matter if you're using Vi or Vim,
  • It may matter if you're using gVim or not (gVim typically has more features enabled at compile time than Vim on the same distribution).
  • It may matter which version of Vim you're on, since a feature may have been added after your version of Vim.
  • It may matter which OS you're on, especially if the plugin calls in external commands.

For most plugins, though, it may not matter.

Fabby
  • 103
  • 4
muru
  • 24,838
  • 8
  • 82
  • 143
  • 12
    It definitely matters if you're using Vi -- the concept of plugins doesn't exist for Vi. Of course, typing vi on any modern box will probably just launch vim with some features turned off. I doubt many people have access to actual Vi these days. – tommcdo Feb 13 '15 at 05:04
57

vim-plug

I like to use the vim-plug plugin manager.

The problem with manually installing a plugin is that it's rather difficult to remove a plugin; you often have several different files in different directories, you have to manually find them & remove them.

Upgrading problems is similarly difficult: What if autoload/old-name.vim gets renamed to autoload/new-name.vim? You now have both an old and new version of a plugin.

vim-plug solves this by storing each plugin in it's own directory; it also includes command to easily install/remove a plugin, so you don't have to muck about with unzipping plugins and the like.

A key advantage of vim-plug over Pathogen is that vim-plug allows you to install and remove plugins more easily. All that Pathogen does is allow each plugin to be in a separate contained directory.

vim-plug relies on git; for MS Windows, you want msysgit.

You can define plugins in your vimrc like so:

call plug#begin('~/.vim/plugged')

" For MS Windows, this is probably better:
"call plug#begin('~/vimfiles/plugged')

Plug 'embear/vim-localvimrc'
Plug 'kchmck/vim-coffee-script'
" ... etc

call plug#end()

Then restart Vim, and then install plugins with:

:PlugInstall

This will put the plugins in ~/.vim/plugged or $HOME\vimfiles\plugged for MS Windows.

You can add this snippet from the FAQ to your vimrc file before the plug#begin() call:

if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall
endif

Note you need curl for this to work. This is almost always available on Linux and OSX, but not on MS Windows; so this trick won't work there...

To remove a plugin, remove it from the vimrc file and run:

:PlugClean

Note that vim-plug doesn't support installing scripts from the Vim scripts website, but those scripts are mirrored on GitHub, so there's no need to do so.

There are also some additional advantages to this such as easier updating of plugin, and on-demand loading for better performance. You can also easily copy your vimrc to another computer, run :PlugInstall, and have all your plugins.

Note there are more plugin managers; I happen to use vim-plug. See also: What is the difference between the vim package managers?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
  • 3
    one of the most important advantage vim-plug has over others is parallel installation/updation of plugins – r3bo0t Nov 17 '16 at 17:05
  • 1
    "those scripts are mirrored on GitHub, so there's no need to do so." Unfortunately the scraper stopped working long ago. https://github.com/vim-scraper/vim-scraper/issues/89#issuecomment-252109015 – Bluu Mar 14 '17 at 19:19
  • @Bluu Yeah, I contacted the maintainer of that last year; there are some technical bits that need updating, and no one really stepping up to do said updating :-/ It's still somewhat useful though, since many scripts aren't updated all that often (especially those that aren't on GitHub). – Martin Tournoij Mar 14 '17 at 20:26
42

Vim 8+ / Neovim

Version 8 introduces a new packages mechanism that largely replaces the need for existing plugin managers (pathogen, vim-plug, vundle, etc.) at the time of writing (2017).

From the documentation:

A Vim package is a directory that contains one or more plugins

A package directory contains two sub-directories:

  • start/ - contains plugins that will be automatically loaded
  • opt/ - contains plugins that are loaded on demand with :packadd

It may seem a bit complicated, but in practice all you have to do is add your plugin here:

                 ↓ package name
~/ .vim / pack / bundle / start / some-plugin
          ↑ packages dir          ↑ plugin dir

On Windows: use ~\vimfiles\pack\ instead of ~/.vim/pack/

By convention, we've used the package name "bundle" as the directory that will contain all our plugins. You can use any name you want, and you can even put your plugins in separate package directories if you really want to.

Example: Installing sensible.vim

mkdir -p ~/.vim/pack/bundle/start
cd ~/.vim/pack/bundle/start
git clone https://github.com/tpope/vim-sensible.git

On Windows: use ~\vimfiles\pack\ instead of ~/.vim/pack/

Next time you start Vim, the plugin will load automatically.

Rich
  • 31,891
  • 3
  • 72
  • 139
Michael Kropat
  • 931
  • 1
  • 7
  • 12
  • 4
    If one's .vim is under version control, it might be better to install plugins with git's submodules; see https://shapeshed.com/vim-packages/#adding-a-package. – Arch Stanton Feb 11 '18 at 22:26
  • 1
    If there was a time-based answer, this would be the current accepted answer as of today. – Amin NAIRI Apr 08 '19 at 18:11
  • Is there also a Neovim part for this explanation? – GWD Dec 29 '20 at 03:09
  • Meh. The "built-in" package system is just a system; but it's not a manager. For example, it provides no way to automatically update a plugin. It's all manual. While it's nice Vim added some support for plugins, management is a key missing feature. You could roll your own update scripts. But why? Vim-plug already does this for you. – fourpastmidnight Feb 12 '23 at 04:06
  • Why do you refer to the bundle/ subdirectory as "package name"? Isn't it a single directory for all packages? 2. Some plugins have two vim files, e.g. autoload/foo.vim and plugin/foo.vim. Do I just keep their internal structure within a ~/.vim/bundle/start/foo subdir?
  • – einpoklum May 01 '23 at 18:08