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?

7 Answers 7

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.

1
  • 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, 2015 at 5: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?

3
  • 3
    one of the most important advantage vim-plug has over others is parallel installation/updation of plugins
    – r3bo0t
    Nov 17, 2016 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. github.com/vim-scraper/vim-scraper/issues/…
    – Bluu
    Mar 14, 2017 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). Mar 14, 2017 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.

5
  • 4
    If one's .vim is under version control, it might be better to install plugins with git's submodules; see shapeshed.com/vim-packages/#adding-a-package. Feb 11, 2018 at 22:26
  • 1
    If there was a time-based answer, this would be the current accepted answer as of today.
    – Amin NAIRI
    Apr 8, 2019 at 18:11
  • Is there also a Neovim part for this explanation?
    – GWD
    Dec 29, 2020 at 3: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. Feb 12, 2023 at 4:06
  • 1. 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 1, 2023 at 18:08
26

Pathogen

Pathogen is a runtime path manager, which loads plugins within Vim. It makes plugin installation simple; here's how it works:

note: If you're using Windows, replace ~/.vim with $HOME\vimfiles.

  1. Copy pathogen.vim to ~/.vim/autoload/pathogen.vim.

  2. Create the ~/.vim/bundle directory, if it does not exist already.

  3. Add the following line to the very beginning of your .vimrc:

    execute pathogen#infect()
    
  4. You're done! When you want to add a plugin, simply copy the entire plugin's directory to ~/.vim/bundle, or git clone it there. For example:

    cd ~/.vim/bundle
    git clone https://github.com/tpope/vim-surround
    
  5. * Caveat: If your plugin comes in the form of a .vim file, it won't work with Pathogen. Copy it to ~/.vim/plugin instead (you may have to create this directory if it doesn't exist).

5
  • 5
    Note: pathogen is not a plugin manager.
    – romainl
    Feb 8, 2015 at 17:48
  • or git submodule add (instead of git clone) if your vim configuration is already part of your own git project. Feb 11, 2015 at 5:53
  • @romainl what is it then? Aug 22, 2017 at 7:05
  • 4
    It's a runtimepath manager. As in it manages the runtimepath option and that's all. It doesn't deal with updating,adding,retrieving,removing plugins at all.
    – romainl
    Aug 22, 2017 at 8:03
  • 1
    Also see @romainl 's excellent Gist about using Pathogen.
    – icc97
    Oct 5, 2018 at 22:01
14

Let us not forget the great & mighty Vundle!

Vundle is a complete plugin manager, with functionality for:

  • Searching for plugins (using :PluginSearch)
  • Installing plugins (using :PluginInstall)
  • Updating plugins (using :PluginUpdate)
  • Managing the plugin load path in the .vimrc (simply comment out plugin entries you don't want to load)
  • Removing plugins you no longer want (using :PluginClean)

As you can see, it's a lot like the vim-plug manager mentioned by Carpetsmoker. I don't know which one came first, but their APIs look very similar to me, as does the setup process.

3
  • The nice thing about Vundle is similar to vim-plug you can store all your plugins in your .vimrc, so when installing on a new server all you need is a Vundle installation + your .vimrc to install everything. Plus you can comment out your plugins and 'clean' them, but be able to comment them back in months or years later when you've long forgotten their names.
    – icc97
    Oct 5, 2018 at 21:50
  • Plus I like Vundle because it's relatively beginner friendly. It gives pretty much only one way of doing things and works in a similar manner to Sublime Text.
    – icc97
    Oct 5, 2018 at 21:57
  • I used to use vundle. It was pretty good. But it's been deprecated for quite some time now. Vim-plug provides an almost 1:1 replacement and is easy to transition to for all those former vundle users like myself. Feb 12, 2023 at 4:08
2

Most likely, your plugin will be a single .vim file.

If this is the case, I searched the help and experimented and figured out the following. From within vim (command mode), type:

:set runtimepath

This will show you a series of directories. Some of these directories will have a plugin subdirectory. Putting any .vim file in one of these plugin subdirectories will get it automatically loaded when you start vim from anywhere.

For me, on Ubuntu linux, I found that /usr/share/vim/vim74/plugin worked well, to install for all users. You may need root permission to copy a file to here (prefix your cp or mv command with sudo).

3
  • 4
    IMHO this is not the best practice. Note that you have vim74, which means that it is version specific, so upgrading to Vim 8.0, will loose that plugin. More upgrade-proof way would be to use /etc/vim/ for that (create it if not present, and add that path to runtimepath if not available in /etc/vimrc)
    – grodzik
    Nov 17, 2016 at 8:03
  • 4
    To add to @grodzik comment: Most likely, your plugin will be a single .vim file I disagree. A huge majority of plugins comes with a .txt file containing the document and you need to generate the tags to get a functional doc. Also a lot of plugins comes with an autoload directory. I think that using a plugin manager is a better solution than managing your runtimepath manually
    – statox
    Nov 17, 2016 at 8:47
  • 1
    -1 /usr/share/vim/vimXX is the stock runtime and is not the place for site configuration/plugins. Instead, site configs/plugins should be placed wherever $VIM points on that system (see :h vimfiles). Usually, this is /usr/share/vim, which is the case for Ubuntu and holds symlinks to /etc/vim. In general, /usr/local/ is the de-facto place for site configuration outside the package manager. Note that Neovim makes use of $XDG_(CONFIG|DATA)_DIRS and site directories, providing better out-of-the-box defaults for site configs.
    – ZeroKnight
    Nov 30, 2018 at 4:46
2

Vire leverages the latest pack format to install plugins and supports Vim and Neovim. You don't need to know where anything goes. Just provide a vimrc and Vire will take care of the rest.

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.