Skip to main content

Vim Advanced

How to Extend Vim's Functionality by Adding Plugins

Vim's functionality can be extended with plugins. Here is how you can install and use plugins in Vim. Also learn about updating and removing the plugins.

Being one of the most popular text editors, Vim also influences other modern text editors. In fact, the Microsoft Visual Studio Marketplace has a Vim extension for VS Code; it has over 4 Million installs.

But what about improving Vim itself? Well, that is possible too! Vim's functionality can be extended using "plugins". So let us see how you can add plugins to Vim.

There are two main ways you can install Vim plugins.

  • The easiest one is by using a plugin manager. This could be considered similar to a package manager, but for Vim.
  • The second way is to manually clone repositories in specific directories.

I would not advise to install plugins manually. The manual method can get confusing and complicated when the number of plugins increases. It may mess up your Vim plugins if you mistakenly clone the plugin in the wrong directory.

I will thus demonstrate how you can add plugins to Vim using a plugin manager.

Vim plugin manager

As I mentioned earlier, using a plugin manager for Vim pays off in the long run when you have multiple plugins. So let's see how to install and use a Vim Plugin manager.

Several Vim plugin managers are available for you to choose from. Some of the most popular ones are:

Since I am using the vimplug plugin manager myself, that is what I will use in this article for demonstration too.

Installing vim-plug plugin manager

To install vim-plug Plugin manager, all you have to do is run either one of the following commands:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

That's it! The vim-plug Plugin manager is now installed.

Installing plugins using the vim-plug plugin manager

For this tutorial, I will use the vim-plug plugin manager to install the following Vim plugins:

To add a plugin, I will first add the following lines to my .vimrc file (or to init.vim for Neovim users):

call plug#begin()

Plug 'junegunn/fzf.vim'
Plug 'preservim/nerdtree'
Plug 'rust-lang/rust.vim'

call plug#end()

There are three distinct sections in the code snippet above.

The first section is call plug#begin(). This line tells Vim that we are beginning a section where we will define all the plugins that we want to install.

The second, third and fourth lines have the pattern Plug 'user/repo'. To understand what this means, we must go back to what plugins I said I would install. The first plugin was fzf.vim. If you go to the given hyperlink it will open the project in Github. If you are somewhat familiar with Github, you will realize that the owner/author of the fzf.vim plugin is 'junegunn', and the project name is 'fzf.vim'. Now, if you look back at the above code snippet, you will notice that user was replaced with 'junegunn' and repo was replaced with 'fzf.vim', the full line being Plug 'junegunn/fzf.vim'. This is how a plugin is specified. I did the same thing to add the rust.vim and NERDTree Vim plugin.

Finally, the last section--call plug#end()--is used to tell Vim that all the plugins are defined and that the plugin section has ended.


Once the plugins are defined in the .vimrc file, they must be downloaded and installed. But do not worry; you don't need to manually download them.

To download and install the plugins, open Vim and then type the :PlugInstall command. Doing so will open up a pane to the left side and you will get a live view of the plugins being cloned and installed.

Installing Vim plugins

Once the installation is complete, you will see the line Finishing ... Done! in the left pane. Close Vim and re-open so that the plugins are cold-loaded.

Your plugins are installed, whoooooo!!!

Updating installed plugins

As you saw, the :PlugInstall command will install all the plugins that are defined in .vimrc or in init.vim. But how do you update the plugins that are already installed?

Well, in that case, use the :PlugUpdate command. Using this command will update all the plugins.

Let's say that out of all the plugins that you have installed, you only wish to update a few, selectively. To update specific plugins, you can add the plugin names after the :PlugUpdate like so:

:PlugInstall plugin0 plugin1

So, to update only the NERDTree plugin, I would run the following Vim command:

:PlugInstall nerdtree

Upgrading the plugin manager itself

A Vim command is provided with the vim-plug plugin manager to upgrade the plugin manager itself. To do so, run the following command:

:PlugUpgrade

Removing installed plugins

To remove a plugin, remove the plugin definition line for the said plugin.

For example, I had fzf.vim plugin defined in my .vimrc file; I will remove that line.

Once that is done, re-open Vim and run the :PlugClean command. That will remove any plugins that are installed but are not listed in the .vimrc file.

:PlugClean

Conclusion

In this article, I demonstrate how you can extend Vim's functionality by using plugins. I also demonstrate how you can update and remove these plugins, including upgrading the plugin manager itself.

I hope you find it helpful in properly using plugins in Vim. Let me know if you have any questions.