How to Manage Plugins in Vim/Neovim (ft. vim-plug)

Vim and Neovim, powerful text editors revered for their efficiency, are further enhanced through plugins that cater to specific programming needs. However, the management of these plugins, updates, and potential issues can become a cumbersome task. This is where vim-plug, a minimalistic plugin manager, steps in. In this guide, we’ll delve into the seamless management of plugins using vim-plug. From effortless installation and updates to hassle-free uninstallation and error detection, we’ll cover it all. Say goodbye to intricate curl, git, and other convoluted processes – with vim-plug, elevating your Vim/Neovim experience is as simple as a few commands.

How to Install vim-plug in Vim/Neovim

Head over to the official junegunn/vim-plug page and download the file plug.vim. Now, put it in the autoload directory.

Download the file plug.vim from official site of vim-plug
Figure 1: Download the file plug.vim from the official site

Autoload Directory:

Create the required directory(ies) if it does not exist.

For vim:

~/.vim/autoload (Unix/Linux)

For Neovim:

~/.config/nvim/autoload (Unix/Linux)

  • For Windows, refer to the official site of vim-plug.
  • For a one-line command to download the file plug.vim into the autoload directory, consult the official site. This can be a bit problematic due to frequent changes in the location of plug.vim, so I cannot provide the command here.

How to Install a Plugin Using vim-plug

I will guide you through using the plug-in luochen1990/rainbow. By the way, this plug-in is used for colorizing matching parentheses with different colors. Visit its site to learn more.

Step 1: Add a vim-plug section

Add a vim-plug section to your .vimrc or init.vim file:

call plug#begin('<auto_load_directory>/plugged')
  Plug '<plugin_1>' 
  Plug '<plugin_2>'
  Plug '<plugin_3>'
  ...
  ...
  ...
call plug#end()

I am choosing the directory <auto_load_directory>/plugged because it does not conflict with any files/directories of vim/neovim. In this directory, all the files related to plugins will be downloaded.

Replace the placeholders plugin_1, plugin_2, etc. with the plugin names you want to install.

For example, to install the plugin luochen1990/rainbow, use:

call plug#begin('~/.config/nvim/autoload/plugged')
  Plug 'luochen1990/rainbow'
call plug#end()

Step 2: Reload $MYVIMRC and Run :PlugInstall

Reload init.vim or .vimrc (or if you encounter confusion or configuration issues after reloading, simply restart your vim/neovim). Now, execute the vim command :PlugInstall in the command line. This command will install the plugins listed in the vim-plug section.

I have automated this step by adding the following snippet after the section:

autocmd VimEnter *
  \  if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
  \|   PlugInstall --sync | q
  \| endif

This snippet will automatically install missing plugins whenever you open any Vim/Neovim window/buffer.

Step 3: Configure Your Plugin

Now, in init.vim or .vimrc, you can add snippets related to the plugin.

For example, for the previous plugin luochen1990/rainbow, I have appended the following:

let g:rainbow_active = 1 "set to 0 if you want to enable it later via :RainbowToggle

Here’s how init.vim or .vimrc will look:

Figure 2: Installing a plugin rainbow using vim-plug and its configuration
Figure 2: Installing a plugin rainbow using vim-plug and its configuration

💡 Recommendation: Split your Vim/Neovim Configuration. This will help declutter it as you install more plugins and configure them.

How to Manage Plugins Using vim-plug

How to Update Plugins Using vim-plug

Execute the vim command :PlugUpdate to update all the installed plugins.

PlugUpdate in vim-plug
Figure 3: PlugUpdate

Alternatively, you can use :PlugUpdate [name …] [#threads] to update specific plugins.

Vim-plug can also update itself. To do that, run the vim command :PlugUpgrade.

How to Uninstall a Plugin in vim-plug

To uninstall a plugin, simply remove the lines corresponding to the plugins you no longer want to use (e.g., Plug '<plugin-names') from the file init.vim or .vimrc, and then run the command :PlugClean.

Figure 4: PlugClean in vim-plug
Figure 4: PlugClean

After running the command, the files and directories corresponding to these plugins will be deleted.

Other vim-plug Commands

  • PlugStatus: Used to check the status of plugins (e.g., if there are any errors or not).
  • PlugDiff: Shows the differences between the previous update (i.e., currently installed plugins) and the pending updates (i.e., latest versions available on the internet).

Conclusion

That covers everything you need to know to use this plugin manager. You can also bind these vim-plug commands (PlugUpdate, PlugUpgrade, PlugClean, PlugStatus, PlugDiff) with leader keys, which will be displayed in vim-which-key. To learn more about vim-which-key, refer to this article.

That’s all, folks. Thank you. If you have any questions, suggestions, or comments, please leave them in the comment section below.

Leave a Comment

Your email address will not be published. Required fields are marked *