VIM Plug – The easy way to install plugins in VIM

Install Plugins In Vim Using Plug

In this module, we’ll learn to use the 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 of editing options and commands but lacks some of the features of a modern text editor.

This is because Vim is a minimal 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 vast community of vim users with many 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 looking to level up their vim experience.

If you are wondering how to get started with vim editor, here’s a great tutorial.

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.

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

So why are we even concerned about Plug?

The Plugin manager can do more than just download 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 required.

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

Prerequisites for vim plug

The vim plugins are generally open-source projects hosted on websites like Github and GitLab. The plugin manager needs to clone the repository before using it, so it is essential to have git installed. To install the plug-in manager, we need to curl too. The installation instructions for different distributions is 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

Here’s a short explanation on create dirs https raw.githubusercontent.com junegunn vim plug master plug.vim

The Vim-Plug plugin manager is downloaded and installed using the command curl -fLo /.vim/autoload/plug.vim –create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim.

The –create-dirs option creates the required directories for downloading a file. It will be created if the autoload directory does not already exist under /.vim/. This location is where the ‘plug.vim’ file for Vim-Plug will be downloaded.

The https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim portion of the command provides the location of the ‘plug.vim’. It instructs curl to download the file from the specified URL and save it in the /.vim/autoload/ directory as ‘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 install tons of plugins in your vim, but choosing the plugins you want from the plugin directory is the essential step. Plugins are generally minimalist in nature, i.e., they provide a 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 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 has no key binding conflict with other installed plugins.

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

Editing the Vimrc

The vimrc file 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, which the user can change according to his will.

The local config file is not present by default; we must create it before editing 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()

Understanding call plug begin, vim plugged, call plug end

call plug#begin() is a Vim-Plug command that initializes the plugin manager and should be placed at the top of your .vimrc file. It initiates plugin management and prepares Vim-Plug to receive plugin installation instructions.

Plug is an additional Vim-Plug command specifying which plugins will be installed. It must be utilized within a call plug#begin() and call plug#end() block in your ‘.vimrc’ file. Following is the syntax for using the Plug command:

Plug 'username/repo-name'

Username is the plugin author’s name, and repo-name is the plugin’s repository name. You can also give the repository URL for a plugin.

After listing all the plugins you wish to install using Plug, you must close the block by calling plug#end(). This indicates to Vim-Plug that you have completed providing the plugins to install.

vim-plugged refers to the directory where installed plugins or stored plugins directory are stored by Vim-Plug. Vim-Plug installs plugins by default in the /.vim/plugged/ directory.

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

:source %

4. Install the Plugin using PlugInstall

After defining which plugins to install using Plug in your.vimrc file, you may install them with the :PlugInstall command in Vim. Instead of manually downloading and installing each plugin using git clone, :PlugInstall automates the process by cloning the plugin repositories and installing them in the ~/.vim/plugged/ directory.

:PlugInstall

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

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

You will 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 discussed earlier, Plug is more than just a download manager of plugins. It offers features that make 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 in handy for dealing with Plug:

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

Summary

Vim is a powerful text editor that is extensible through plugins. While Vim lacks some of the features of modern text editors, it makes up for it by being a minimalist program that is efficient and mature. Vim-Plug is a popular plugin manager for Vim that offers many features, including controlling the versions of your packages and lazy loading of packages. Installing Vim-Plug is straightforward, and many plugins on Awesome Vim can enhance your Vim experience. By following the steps outlined in this tutorial, you can easily add and configure plugins to Vim using Vim-Plug.

To get detailed knowledge about any vim plugin, read the Readme of the plugin repository. You can head on to the vim-plug repository to learn more about vim-plug.

Vim-plug is not the only plugin manager out there for vim editor, do check out vundle, which is another plugin manager for vim with a different approach to managing the plugin.

Stay tuned for vim hacks and tutorials.

What is Vim-Plug Manager and how does it differ from other package managers?

The Vim-Plug Manager is a minimalist Vim plugin manager designed to be fast, efficient, and easy to use. It differs from other package managers by its simplicity in installation and usage.

How can I update plugins using the Vim-Plug Manager?

Updating plugins with the Vim-Plug Manager is straightforward – you can simply run the command to update all installed plugins to their latest versions.

Can I easily remove plugins with the Vim-Plug Manager?

Yes, removing plugins with the Vim-Plug Manager is as easy as specifying their names in the Vim configuration file and running the removal command.

How do I install new plugins using the Vim-Plug Manager?

Installing plugins with the Vim-Plug Manager is a quick and efficient process. You just need to specify the plugins’ GitHub repository URLs in the Vim configuration file and run the installation command.

Can I manage Vim plugins manually instead of using a package manager?

Yes, you can manually install and manage Vim plugins without a package manager by downloading the plugin files and placing them in the appropriate directories in your Vim setup.

Is the Vim-Plug Manager compatible with Linux and Unix-based systems?

Yes, the Vim-Plug Manager works seamlessly on Linux and Unix systems, making it a versatile tool for managing Vim plugins across different platforms.