VIM Plug – The easy way to install plugins in VIM

Install Plugins In Vim Using Plug

In this module, we’ll learn to use VIM plug to install plugins. Vim is one of the best text editors out there. It is very mature, efficient and extensible. It offers a wide range set of editing options and commands but lacks some of the features that a modern text editor has.

This is because Vim is a very small program and is minimalist by design. It is meant to run on machines with low computing and storage. But this does not mean we cannot have extra features in vim.

There is a huge community of vim users that have come with a lot of plugins that can help us customize vim and its functionalities to our liking. This tutorial covers how we can use Vim-Plug to install new plugins in Vim and is meant for new vim users who are looking to level up their vim experience.

Why do you need VIM Plug?

Truth be told, you do not need a plugin manager to install plugins in vim. There is a well-known way to install vim without any plugin manager, which is beyond the scope of the article.

In fact, there was no official way of installing vim plugins until 2008 when vim Plug was accepted as the way to install plugins by the community.

So why are even concerned about Plug?

The Plugin manager can do more than just downloading packages – it can control versions of your packages, keep them updated and provides a feature called the Lazy plugin, which lets us load a package only when it is required.

So let us discuss how to add a plugin to vim using the Plug.

Prerequisites for vim plug

The vim plugins are generally open-source projects and are hosted over websites like Github and GitLab. The plugin manager needs to clone the repository before using it, so it essential to have git installed. To install the plug-in manager we need curl too. The installation instruction for different distribution are as follows:

-# For Debian Based distributions like Ubuntu
sudo apt install git

# For Fedora or RPM-based system
sudo dnf install git

# For Arch-based distributions
sudo pacman -S git

Installing the Vim-Plug

To install the vim-plug, run the following command in your terminal. It installs the plug manager for vim.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
VIM plug
Fig 1: Installing Plug-Vim

Now that you have a plugin manager installed, you can select the plugins you want to install in your vim.

Selecting the Plugins

You can have tons of plugins installed in your vim, but choosing the plugins that you is the most important step. Plugins are generally minimalist in nature i.e. they provide the set of features dedicated to adding a single feature or solving a single problem.

There are a lot of plugins available for each feature/problem that you can possibly imagine, but choosing which exact plugins from the set of plugins that provide similar features can be an uphill task.

To make life easy there is a website called Awesome Vim that provides a gist of all these plugins in a single place. It is generally a good option to check if the plugin is maintained properly and if the plugin has no key binding conflict with other installed plugins.

Here is a set of plugin that you can choose for these specific features –

Editing the Vimrc

The vimrc is the configuration file for Vim that is loaded every time we launch vim.

There are two vim config files – the global config, located in $VIM which generally should not be manually changed and the local config file that the user is free to change according to his will.

The local config file is not present by default, we need to create it before we can edit it.

" Create a blank .vimrc file if you don't have one
touch ~/.vimrc

" Open the .vimrc file for making changes
vim ~/.vimrc

You need to follow the next steps for installing a plugin:

1. Mention the plugin to be installed by using the following syntax: Plug'<Link to plugin repository>'

" Example Plug Syntax
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" For GitHub repositories, you can just mention the username and repository
Plug 'junegunn/vim-easy-align'

2. All the plugins mentioned using Plug should be enclosed within call plug#begin() and call plug#end(). Your vimrc should look something like this:

call plug#begin()

Plug 'tyru/open-browser.vim' " opens url in browser
Plug 'http://github.com/tpope/vim-surround' " Surrounding ysw)
Plug 'https://github.com/preservim/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'https://github.com/ap/vim-css-color' " CSS Color Preview
Plug 'https://github.com/tpope/vim-commentary' " For Commenting gcc & gc

call plug#end()

3. Save the file and source the file. The file can be sourced using the following command:

:source %

4. Install the Plugin using PlugInstall

:PlugInstall

A sidebar will open up which will clone the repositories in ~/.vim/plugged.

Vim PlugInstall
Fig 2: Installing Plugins using the command :PlugInstall

You will the see the changes made by the plugins after restarting vim.

Vim Nerd
Fig 3: The nerd tree plugin file-manager can be accessed using the command :NERDTreeToggle

Advanced configuration of Plug

As we discussed earlier, Plug is more than just a download manager of plugins. It offers features that makes it easier for the user to handle plugins, here are some of the features along with example plugins to use them with.

Lazy Loading

" Nerdtree is only loaded when NERDToggle command is pressed
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }

" fireplace.vim is activated only when we use clojure as a programming language
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

Different Branch

" Uses the non-default branch, stable of the package
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

Local Repository

" Use your own local plugin that is not version controlled by Plug
Plug '~/local-plugin'

The following set of commands come handy for dealing with Plug:

Image 1
Fig 4: Different commands available in vim-plug

Conclusion

Now you can now download any plugins and customize vim using them. To get detailed knowledge about any vim plugin you just read the Readme of the plugin repository. To know more about vim-plug you can head on to the vim-plug repository. Stay tuned for vim hacks and tutorials.