Tools Vim Plugins Lead image: Lead Image © Thomas Reichhart, 123RF.com
Lead Image © Thomas Reichhart, 123RF.com
 

Managing Vim plugins

Plug and Play

We look at some tools that can help take the pain out of managing plugins. By Bruce Byfield

The Vim text editor [1] is one of the most popular pieces of free software ever written. Since its first release in 1991, Vim has become available for all major operating systems. It is installed by default on most Linux distributions, and, in the 2016 LinuxQuestions Reader Choice Awards, outpolled its long-time rival Emacs by more than three to one – being the first choice of more than 30 percent of responders [2]. Much of this popularity is due to the degree of customization created by its plugins – almost 1,600, according to the VimAwesome site [3], and even that number may be low.

Most users, of course, use fewer than a dozen Vim plugins. However, even that number can be difficult to delete, although that is probably unnecessary with a recent version of Vim. The truth is, Vim's original design was not intended for plugins. Probably no one is using the oldest plugins, which needed to be unarchived in a user's home directory and were only recognized when the command :helptags was run from within Vim. Yet even the default method of installation – dumping all plugins in ~/.vim/plugin – can make knowing what to update or delete difficult, even with just a few plugins.

To make installing easier, four plugin managers have been developed: vim-addon-manager, Pathogen, and its enhancements Vundle and Neobundle. Which of these tools you should use depends on the services you prefer, but all take much of the pain out of managing plugins

Vim-addon-manager

Vim-addon-manager [4] is a Linux tool available in many distributions, including Debian, Ubuntu, and Fedora. Unlike the other solutions, vim-addon-manager is installed outside of Vim as a regular package. Typically, it is installed with another package that contains two dozen of the most popular plugins. The command has a registry of plugins in /usr/share/vim, a source directory at /usr/share/vim/addons, and a installation target at $HOME/.vim for private use or /var/lib/vim/addons for all system users. These directories can be changed using the options -r, -s, or -t, respectively.

To view available plugins, enter vim-addons list. Each plugin has a status of installed, removed, disabled, broken, or unavailable, which can be checked with the command vim-addons status. Plugins are added to vim-addons with the command vim-addons install [PLUGIN] (Figure 1).

Vim-addon-manager controls plugins from within a distribution's standard package system.
Figure 1: Vim-addon-manager controls plugins from within a distribution's standard package system.

Pathogen

Unlike vim-addons, Pathogen [5] is itself a Vim plugin, working from within Vim to manage other plugins. It is best run with GitHub as a source, because many plugins are hosted there. Pathogen's innovation is that it places all the files for each plugin within a separate directory, making them easy to find for maintenance. However, unlike Vundle and Neobundle, it does not upgrade or delete files automatically.

Because Pathogen uses a different structure from Vim's default, to avoid any problems, before installation, you should rename the existing .vim directory (or, in Windows, ~\vimfiles) and .vimrc configuration file in your home folder and replace them with new ones.

To install Pathogen, log in to GitHub and run the command:

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

These commands create the necessary directories for Pathogen and install the plugin.

Next, create a new .vimrc file and add to it:

execute pathogen#infect()
syntax on
filetype plugin indent on

These lines start Pathogen before any other plugin so that it can manage them. Optionally, you can add:

call pathogen#runtime_append_all_bundles()
call pathogen#helptags() "

to update the help automatically each time Vim starts. To add other plugins from GitHub, change directories to ~/.vim/bundle and run:

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

Should you be using another source for plugins, adjust the command accordingly. You can use the old files as a list of plugins to reinstall under Pathogen's management before deleting them.

Vundle

Vundle [6] (short for "Vim bundles," the name it uses for plugins) is an enhancement of Pathogen. Like Pathogen, Vundle rearranges the directory structure to make management easier. However, Vundle updates plugins automatically and adds several utilities as well.

Windows users have a graphical interface for installing and a choice of whether to run Vundle in a Bash shell – if one is installed – or from a Windows command prompt. Probably, they will need to install curl as well.

On Linux, however, installation is only a matter of running:

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

Notice that, unlike Pathogen, Vundle is installed to a sub-directory of .vim/bundle, just like any other plugin.

To update plugins automatically with Vundle, add to .vimrc the following lines (see also Figure 2):

set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Vundle automatically updates the plugins listed in .vimrc.
Figure 2: Vundle automatically updates the plugins listed in .vimrc.

These lines set the run-time path to Vundle, initiate it, and set Vundle to manage itself. Below the last line, add the other plugins that Vundle manages, one per line, starting with Bundle, for example:

Bundle 'tpope/vim-fugitive'

The GitHub Vundle page gives examples of how to list other sources that you might use.

Installing Vundle also installs four utilities that are run from within Vim. Their names are self-explanatory. Bundlelist lists configured plugins, BundleInstall installs or updates a plugin, BundleSearch searches for a plugin, and BundleClean removes plugins with a confirmation message. The BundleInstall, BundleSearch, and BundleClean commands are completed by the name of a plugin.

Note that future versions of Vundle will replace the use of "Bundle" with "Plugin." BundleInstall, for instance, will be renamed PluginInstall.

NeoBundle

Just as Vundle builds on Pathogen, so NeoBundle builds on Vundle. In the past, NeoBundle was difficult to use, because it was in rapid-development and detailed instructions were only in Japanese. More recently, however, development has slowed to maintenance only, and English instructions are available.

NeoBundle [7] has all the features of Vundle, although with different names for its utilities. Additionally, it has several advantages. To start with, it supports Subversion or Mercurial repositories, while Vundle supports only Git. Moreover, it can be locked so that it only uses a specific version of Vim, which can be used to avoid having updates break your collection of Vim plugins.

Most importantly, NeoBundle integrates with other extensions written by its creator Shougo Matsushita. Advanced users might be especially interested in this feature because of Unite.vim [8], a plugin for searching files, yanking (copying and pasting), and manipulating buffers. If such features interest you, then you might prefer NeoBundle over Pathogen or Vundle.

NeoBundle requires Vim 7.2.051 or higher. Integration into other plugins may require or recommend other dependencies – for example, vimproc for use with Unite.vim. To install on either Linux or Windows, run the commands:

mkdir ~/.vim/bundle
git clone https://github.com/ Shougo/neobundle.vim ~/.vim/bundle/neobundle.vim

You can copy the sample .vimrc file from NeoBundle's GitHub page. However, if you do not want the five plugins it mentions, instead you can add the lines shown in Listing 1 to .vimrc instead. These lines assume you are using GitHub. To use another site, add its full URL.

Listing 1: Modifying .vimrc

01   if has('vim_starting')
02    set nocompatible
03      set runtimepath+=~/.vim/bundle/neobundle.vim/
04  endif
05
06 call neobundle#begin(expand('~/.vim/bundle/'))
07
08  NeoBundleFetch 'Shougo/neobundle.vim'
09
10  call neobundle#end()
11
12 filetype plugin indent on

To install other plugins from GitHub, use the following command structure:

curl -k https://github.com/[MAINTAINER]/[PLUGIN PATH] > ~/.vim/bundle/[PLUGIN PATH]

When you install the first plugin, check that it adds a line for it below the line NeoBundleFetch 'Shougo/neobundle.vim'. For example:

NeoBundle 'Shougo/vimshell'

or, if you want to pin NeoBundle to a specific version number:

NeoBundle 'Shougo/vimshell', { 'rev' : 'REVISION-NUMBER' }

NeoBundle's utilities are:

These utilities are available in slightly different forms when Neobundle and Unite.vim are run together. For more information, run the command :help neobundle from within Vim.

Deciding How to Manage

Which of these plugin managers you should use depends on your needs and preferences.

On the one hand, Vim-addon-manager simplifies maintenance by integrating Vim updates with standard package management and requires no manual editing to set up. On the other hand, it does not update automatically or include the utilities offered by Vundle and NeoBundle.

Similarly, although the updates offered by Vundle and NeoBundle are convenient, for security's sake, you may not want automatic updates. If you are dealing with plugins still being rapidly developed, you may prefer NeoBundle over Vundle, because it can pin each plugin to a particular version. Neobundle could also be preferable if you are using a non-Git repository.

Still, between the four plugin managers, you should be able to find at least one that suits you. The truth is, if you use more than a couple of plugins, any of them should be better than Vim's default handling of plugins.