Managing Vim plugins

Added Features

Article from Issue 172/2015
Author(s):

Managing plugins is a requirement for Vim users, and plugin managers can make the task easier. We look at four options.

Vim is not a text editor so much as a way of life. Vim, which traces its lineage back to Vi, has accrued plugins during its 36-year history the way offshore rocks accrue barnacles. From file managers and spell checkers to multiple-entry clipboards and undo levels, Vim plugins ingeniously offer from the command line every utility you can imagine on the desktop – and probably some that you can't. Regular users can be known to install more than a dozen at a time, which makes managing them a basic requirement.

The trouble is, Vim was never made to handle the large number of plugins that many modern users install. Without a manager, a plugin either has to be dropped into ~/.vim/plugin, or, in the case of very old plugins, simply ~/.vim. However, its files are unsorted, which means you have to update or delete each plugin manually. In some cases, you also need to run the command :helptags if you want online help.

Plugin managers are designed to spare users these trials. Vim-addons [1] treats plugins as packages, whereas Pathogen [2] – itself a plugin – creates separate directory structures for each plugin under ~/.vim, making updating and deleting much easier. By contrast, Vundle [3] and NeoBundle [4] rely on Pathogen's structure but add automatic updates and other features. Each of these alternatives is considerably easier than adding plugins to unmodified Vim, but which to choose depends on your work habits, as well as your notions of security.

Vim-addons

Vim-addons is unique among Vim managers in that it operates entirely outside Vim, treating plugins as packages and using a command syntax very similar to Debian's apt-get, with commands and subcommands. On distributions where it is available, such as Debian, Fedora, and Ubuntu, its own package is called vim-addon-manager. (Notice that the command itself is simply vim-addon.) Distributions that include vim-addon-manager often include vim-scripts [5], which downloads about 25 common Vim plugins that vim-addon-manager can then install.

Vim-addons manages plugins that are stored in /usr/share/vim/addons, where they are conveniently organized into categories by subdirectories, registering them in the directory /usr/share/vim/registry/ and installing them in ~/.vim for an ordinary user or in /var/lib/vim/addons for all users.

Typing vim-addons list presents the available plugins (Figure 1). Adding the sub-command files followed by a plugin name shows the files associated with a registered plugin, whereas show plus a plugin gives more detailed information about a registered plugin.

Figure 1: Vim-addons manages plugins as though they were packages. Here, Vim-addons displays a list of available plugins.

To active a plugin for the current account with Vim-addon, download it and uncompress it if necessary, then enter the following command while logged in as root:

vim-addons install [PLUGIN PATHS]

Removing a plugin uses a similar structure:

vim-addons remove [PLUGIN]

From a non-root account, you can use the sub-command disable to make a plugin unavailable to that account or use enable to make a plugin available without affecting whether other users can use it.

The advantage of Vim-addons is that you need to know nothing about Vim to use it, and you do not even need to edit text files for configuration. You may have to look up registered plugins online to see what they do, but that is no different from any other plugin manager.

Pathogen

Pathogen was the first Vim plugin for managing other plugins. Pathogen's major innovation is a directory structure that places all the files for each plugin in its own subdirectory. With Pathogen, you still need to upgrade or delete plugins manually, but you no longer have to check which files belong with which plugin; nor do you need to worry about naming conflicts.

This directory structure is so different from Vim's default directory structure that, before you install Pathogen, you should delete any ~/.vim directory or ~/.vimrc file in your home folder – or, possibly, move them so that you have a list of plugins you will want to reinstall once Pathogen is configured.

Create an empty .vim and .vimrc and use mkdir to create the new subdirectories ~/.vim/autoload and ~/.vim/bundle. The autoload subdirectory contains plugins that you always want to load with Vim, and the bundle subdirectory contains a separate directory for each plugin's files ("bundle" being Pathogen's alternative name for plugins).

To install Pathogen, you can create the directory ~/.vim/autoload/pathogen.vim and drop the plugin into the new directory. If you want the latest version, make sure curl is installed and create a GitHub account for yourself. Then run the commands:

mkdir -p ~/.vim/autoload   ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

Both of these installation methods position Pathogen so that it loads automatically each time you start Vim.

However, to finish installing Pathogen, you also need to add the lines in Listing 1 to a clean ~/.vimrc. These lines are the bare minimum you need to use Pathogen. They prevent Vim from treating Pathogen as simply another plugin and load Pathogen before any other plugins so that it can manage the rest.

Listing 1

.vimrc for Pathogen

 

If you choose, you can also add the line call pathogen#helptags() so that Vim automatically generates new online help files each time it starts, freeing you from manually updating the help files each time you add a new plugin. However, be warned that if you have multiple plugins, Vim may take a few seconds longer to start with this option.

At this point, Pathogen is configured for use. To add a plugin, add a new directory for it under ~/.vim/bundle/ and place the plugin in the new directory. You can take advantage of GitHub to use the latest version of the plugin, using the command structure:

git clone git://github.com/[WRITER]/[PLUGIN PATH] ~/.vim/bundle/[PLUGIN PATH]

These days, you should find few plugins that are incompatible with Pathogen. However, if you do, you have a good chance of finding another plugin that does much the same thing. Pathogen is useful enough that, if you have to choose between Pathogen and another plugin, whenever possible you should keep Pathogen and discard the other plugin.

Vundle

Vundle is an enhancement of Pathogen. It uses the same directory structure as Pathogen but automatically updates plugins and includes several utilities for use within Vim. It also offers clear, concise instructions.

The quickest way to install Vundle is to clone the latest version on GitHub:

git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

At the minimum, Vundle also requires the lines shown in Listing 2 in .vimrc.

Listing 2

.vimrc for Vundle

 

To update plugins automatically, you can place other plugins, one per line, between the call vundle#begin() and call vundle#end() lines and below the Plugin 'gmarik/Vundle.vim' line. Vundle assumes that you are downloading from GitHub, so only the repository's owner and the plugin's file name are needed to enable automatic updates. If you are using another repository, the full path is needed, as illustrated in the sample .vimrc on Vundle's homepage [3].

When using Vim, install other plugins just as you installed Vundle, creating a Vim clone in a subdirectory of ~/.vim/bundle/. Plugins are enabled by running vim +PluginInstall +qall from the command line or :PluginInstall from inside Vim. Both commands enable all plugins added since the last time one of them was run.

Also included in Vundle are three other utilities that work from within Vim:

  • :BundleList: Lists all plugins being managed by Vundle.
  • :BundleClean: Removes unused plugins. The basic command asks for confirmation of each deletion, whereas :BundleClean(!) removes plugins without any input.
  • :PluginSearch [STRING]: Locates all plugins whose file names contain the entered string and opens a list in a new window from which you can install or remove them. If you install, you still need to add a line to .vimrc to enable the plugin. :PluginSearch requires curl to work (Figure 2).
Figure 2: Among Vundle's utilities is :PluginSearch, which locates all plugins that match the entered string.

As you can see, Vundle adds a new level of functionality to Pathogen, making plugin management even simpler.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News