Vim is a highly customizable and modular editor. It can do a lot, by default. If there’s a certain feature that’s missing in Vim, chances are there’s already a plugin available for it. Of course, we can write our own functions and plugins, but it’ll be a waste of time.
Not only that, the most-used plugins are well-tested and maintained. Oftentimes, third-party plugins perform better than the built-in functionality of Vim.
In this section, we’ll go through the different steps to install Vim plugins.
Vim doesn’t have any built-in plugin management mechanism. Therefore, we’ll need to handle the installation and uninstallation of plugins manually.
A Vim plugin comes in three forms:
- a single .vim file
- a Vimball file
- a set of files and directories that follow a structure
2.1. Installing a .vim File
By default, Vim expects us to place our config and plugins in the ~/.vim directory. For a single .vim plugin, we’ll need to place it in the ~/.vim/plugin directory.
2.2. Installing a Vimball File
A Vimball is a Vim-based archive. It has the .vba extension.
A Vimball file can be opened in Vim. Afterward, we can source it with the source command:
$ vim datetime.vba
The so command is an alias for the source command. The % sign indicates the current source file.
Once we source the file, it moves the file into the appropriate ~/.vim directory. Afterward, we can delete the .vba file.
2.3. Installing a Set of Plugin Files
There are complex Vim plugins that are made up of multiple files. Therefore, we’ll need to follow a certain structure as of Vim 8.
The Vim 8 way of package management ensures safe organization, which is free of file conflicts. Apart from that, it also eliminates the need for plugin managers.
Let’s go over the conventional Vim plugins directory structure:
`-- pack `-- vendor |-- opt | `-- baz | |-- plugin | `-- syntax `-- start |-- bar | |-- autoload | | `-- bar.vim | |-- plugin | `-- syntax `-- foo |-- doc | `-- foo.txt |-- plugin | `-- main.vim `-- syntax
Let’s break it down:
- pack is the top-level directory inside ~/.vim
- vendor is the name of the package publisher
- opt and start signify when the plugin can be used
- foo, bar, and baz are the actual plugins
- autoload/bar.vim is loaded when the bar command is used
- foo/plugin/main.vim is loaded soon after Vim starts
- foo/doc contains the plugin help files
Additionally, plugins inside the start directory will load upon running Vim. In contrast, the opt directory contains optional plugins. We can load these plugins with the :packadd command.
As an example, let’s install the Fugitive plugin, which is a wrapper around Git.
First, we’ll create the respective directory:
$ mkdir -p ~/.vim/pack/tpope/start/fugitive
Next, we’ll clone the Fugitive repository into the start/fugitive directory:
$ git clone --depth=1 https://github.com/tpope/vim-fugitive ~/.vim/pack/tpope/start/fugitive
Vim finds the plugin and loads it automatically every time we run Vim.
NeoVim is a heavily modified fork of Vim. It adds a lot of new features and enhancements to Vim. However, most of the Vim plugins and configs are compatible with NeoVim.
3.1. NeoVim Plugins Directory
NeoVim stores its config and plugins under the $XDG_CONFIG_HOME/nvim directory.
Conventionally, the XDG_CONFIG_HOME environment variable references ~/.config. Thus, the full path to our NeoVim config is ~/.config/nvim.
3.2. Plugin Installation
Vim and NeoVim follow the same package installation mechanism.
All we need to do is create the appropriate directories under ~/.config/nvim/pack directory. Then, we can clone the plugins into their respective directories.
Plugin managers reduce the overhead of installing, removing, and updating Vim plugins.
vim-plug stores plugins in their own directories. It provides easy commands for operations such as installing, upgrading, removing, and cleaning plugins. It depends on git.
4.1. Installing vim-plug
Installing vim-plug is similar to installing any other plugin. However, we can place the following snippet in our ~/.vimrc file that will automatically install vim-plug once we run Vim:
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
This runs only if vim-plug isn’t already installed.
4.2. Installing Plugins
Plugins installation is pretty straightforward. We specify the required plugins in the vim-plug block inside ~/.vimrc:
call plug#begin('~/.vim/plugged') Plug 'vim-airline/vim-airline' Plug 'morhetz/gruvbox' Plug 'tpop/fugitive' call plug#end()
Afterward, we can simply run the :PlugInstall command to install the plugins defined in the block. As we add more plugins, we’ll need to run this command.
4.3. Removing a Plugin
We can remove a plugin by omitting it from vimrc. Afterward, we can run the :PlugClean command.
It deletes all the files and directories related to the plugin.
4.4. Upgrading vim-plug
We can upgrade the vim-plug plugin itself by running the :PlugUpgrade command.
In this article, we discussed how to install plugins for Vim and NeoVim. First, we learned how to install plugins manually. Then, we went over the vim-plug manager for easier plugin management.