vim

How to Install the Vim Plugins

A text editor that is frequently used in the command-line situations is called Vim (Vi Improved). It is renowned for its effective keyboard shortcut implementation and simultaneous usage of several documents. A Vim plugin is a collection of Vim scripts that add a functionality to the text editor. These scripts can offer many features like syntax highlighting, new commands, and file type compatibility. Vim plugins are typically distributed as a package and can be easily installed and managed using a plugin manager. With numerous plugins available for added functionality, Vim is a text editor that is extremely adjustable and adaptable. In this guide, we will show you a method to install the Vim plugins in Linux.

Check the Vim Version

First, we take a look at the version command to get the version of a “Vim” editor installed. If the command shows the output like the one in the following, it means that your system has no “Vim” editor installed. Also, it gives you some command suggestions for its installation.

omar@virtualbox:~$ vim --version
Command 'vim' not found, but can be installed with:
sudo apt install vim         # version 2:8.2.3995-1ubuntu2.3, or
sudo apt install vim-tiny    # version 2:8.2.3995-1ubuntu2.3

 

Just like that, if you try to open some file with “vim”, it won’t allow you and shows you the following output along with some command suggestions like “vim not found”:

omar@virtualbox:~$ vim pass.txt
Command 'vim' not found, but can be installed with:
sudo apt install vim         # version 2:8.2.3995-1ubuntu2.3, or
sudo apt install vim-tiny    # version 2:8.2.3995-1ubuntu2.3

 

Install the Vim Editor

To get Vim in your Linux system, you have to try using the following-attached command using the apt package manager. The system downloads two packages, “vim” and “vim-runtime”, which are both new to the system. The command prompts the user to confirm the installation by typing “y” and hitting enter. Vim editor is installed in less than 10 seconds.

omar@virtualbox:~$ sudo apt-get install vim

 

Set Vim

To set the Vim editor according to your needs, you can use its configuration file which is vimrc. To open the vimrc file, we use the “nano” editor with sudo rights. After adding some commands to it, we save it and open it once again to reflect the changes.

omar@virtualbox:~$ sudo nano ~/.vimrc

 

Install Curl and Git

It’s necessary to install curl and Git in your Linux system before installing the Vim plugins. Therefore, we use the apt-get instruction with the “install” keyword followed by the package name. The command is executed and both the packages are installed smoothly.

omar@virtualbox:~$ sudo apt-get install curl git

 

Install the Vim Plugin Manager

Here, we create two directories, “autoload” and “bundle”, within a directory called “.vim” in the user’s home directory. The “mkdir” command is used to create the directories. The “-p” option tells the command to create any parent directories in the path if they do not already exist. After this, you need to move within the bundle folder of Vim using the following “cd” instruction:

omar@virtualbox:~$ mkdir -p ~/.vim/autoload ~/.vim/bundle
omar@virtualbox:~$ cd ~/.vim/bundle
omar@virtualbox:~/.vim/bundle$

 

There are several ways to install Vim plugins, but one of the most popular methods is to use a plugin manager i.e. tool that makes it easy to install, update, and manage Vim plugins. The “curl” command is installing a Vim plugin manager called “vim-plug”. Option -f tells curl to fail silently if the HTTP server returns an error code, -L tells the curl to follow any redirections, -o tells curl to save the output to a file named “~/.vim/autoload/plug.vim”. and –create-dirs tells curl to create the directories specified in the -o option if they don’t exist. The file being downloaded will be saved in the “~/.vim/autoload” directory. Once the command is executed successfully, the vim-plug is installed and ready to use in Vim.

omar@virtualbox:~/.vim/bundle$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

 

Install the Vim Plugins

The manual way of installing the Vim plugins is to use the simple Git command to clone the official package of the repository. Let’s say, we want to install the nerdtree Vim plugin and use the Git clone command followed by the directory where the repository should be cloned. The progress shows that the repository is cloned into the specified directory. The result displays the repository’s download status and download speed.  Once the command is executed successfully, the nerdtree plugin is installed and is ready to use in Vim.

omar@virtualbox:~/.vim/bundle$ git clone https://github.com/preservim/nerdtree.git/ ~/.vim/pack/vendor/start/nerdtree

 

The other way to install the Vim plugins is to use the Vim plugin manager, “vim-plug”, via the Vim configuration file, “vimrc”. This is a configuration code for Vim’s plugin manager that is used to install and manage the plugins for Vim. Open the vimrc file and write the following code in it. The code starts by calling the “plug#begin()” function. Start the configuration of the plugins that are managed by the vim-plug. After that, the code uses the “Plug” command followed by the name of the plugin which is “preservim/NERDTree”. This command tells the vim-plug to install the plugin from the specified “preservim/NERDTree” repository. Finally, the code calls the “plug#end()” function to end the configuration of the plugins that are managed by the vim-plug and it should proceed with the installation of the specified plugins. After this code is executed, the NERDTree plugin is installed and is ready to use in Vim.

There is a command that is used to install the plugins that are specified in the vim-plug configuration. The “:PlugInstall” command tells the vim-plug to install any plugins that are specified in the configuration but have not yet been installed. Use this command in the “insert” mode of the Vimrc file. It downloads the plugin from their repository. After this, it installs the Nerdtree plugin and makes it ready to use in Vim.

The installation process is displayed at the right panel of the vimrc file after executing the “:PlugInstall” instruction as shown in the attached image where the NERDTree plugin is installed:

If you want to update the already installed plugin of Vim, use the “PlugUpdate” instruction in the insert mode of Vim followed by the name of a plugin as shown in the following:

The output shows that the NERDTree plugin is already up-to-date.

Conclusion

We explained the use of Vim editor and its plugins within the introduction. Also, this guide is the bundle of commands to install the Vim plugins using two different methods: using the Git clone instruction and the vim-plug plugin manager. Using both ways, we demonstrated how a Vim plugin like NERDTree can be installed in the Linux system.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.