This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Introduction to Vim Plugins

2:14 Tools lesson by

Vim has the ability to extend its functionality through plugins that can be installed either manually or through plugin managers. We'll setup the vim-plug manager to install plugins for our .vimrc


egghead.io comment guidelines

Avatar
egghead.io

Vim has the ability to extend its functionality through plugins that can be installed either manually or through plugin managers. We'll setup the vim-plug manager to install plugins for our .vimrc

Avatar

I'm having problems automating the plugin installation:
Using macOS,

$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct  5 2016 16:04:34)
$ which vim
/usr/bin/vim
$ python --version
Python 2.7.12
$ # from https://github.com/junegunn/vim-plug#installation
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
call plug#begin('~/.vim/plugged')

Plug 'tpope/vim-sensible'
Plug 'junegunn/vim-easy-align'

call plug#end()
autocmd VimEnter *
 \ if !empty(filter( copy(g:plugs), '!isdirectory(v:val.dr)' ))
 \|     PlugInstall | q
 \| endif

$ vim
Error detected while processing VimEnter Auto commands for "*":
E716: Key not present in Dictionary: dr)
Press ENTER or type command to continue
Error detected while processing VimEnter Auto commands for "*":
E116: Invalid arguments for function isdirectory(v:val.dr)
In reply to egghead.io
Avatar
Ziya

you have a typo, v:val.dr should be v:val.dir

In reply to

Vim's scripting ability allows you to use external files to enhance the Vim's functionality. To add plugins, you can either manually add them to your file system, or you can use a plugin manager to automate it.

In our case, we'll use the Vim-plug plugin manager. Vim-plug gives us a simple way to declare what plugins we want to load, to allow in session through Git, and then reload the editor with the plugins added.

Vim-plug does have a few requirements, so be sure to read over the documentation for your platform. To get started, we'll first need to copy the Vim-plug script into our .Vim directory. With that installed, let's open r.mrc and add a few lines to the file.

We'll add call plug begin, and then say a directory where we want our plugins to be installed. Then we'll add plug and tell Vim-plug that we're done trying to load any plugins.

So, [inaudible 1:05] will add a simple plugin, Vim sensible, which just sends a bunch of normal defaults for us. We'll use the command plug and give it the GitHub user name, and then the repo we want to load. In this case, we're loading a repo from tpope and his plugin, Vim sensible.

Now we can save and quit Vim, start the editor back up again, and execute plug install.

We'll get a small window on the left saying that we're installing the plugin. As soon as the plugin installation is done, Vim will update and we'll have the plugin running. We'll enter command mode, and use the command exit to close the installation window.

We can take this a step further with a few additional lines of Vim script. This autocommand will run whenever Vim starts. If there is a missing plugin, it'll run the command, plug install, and then install whatever is missing.

We'll add a new plugin to test it out. As you can see, the plugin installed without us having to enter anything, and then the window was closed as soon as it was done.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?