Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Meet minpac

#70

Run time:

Minpac is a minimal package manager for Vim 8. It makes it easy to add plugins, keep them up to date, and remove them. In this video, we’ll see how it works.

Shownotes

Installing minpac

Minpac should be installed as an optional plugin, in a package called minpac:

$ mkdir -p ~/.vim/pack/minpac/opt
$ cd ~/.vim/pack/minpac/opt
$ git clone https://github.com/k-takata/minpac.git

Add these lines to your vimrc file:

packadd minpac
call minpac#init()

That’s it. Now you can use minpac to install other plugins.

Registering plugins

You can register plugins with minpac by calling the minpac#add() function. The first argument is a string representing the author/repo-name on github. The optional second argument can be a dictionary of options.

For example, add these lines to your vimrc:

call minpac#add('tpope/vim-surround')
call minpac#add('k-takata/minpac', {'type':'opt'})

That registers Tim Pope’s surround plugin, as well as minpac itself. By default, minpac installs plugins to the pack/minpac/start directory. By specifying {'type': 'opt'}, you can make minpac install a plugin to the pack/minpac/opt directory.

Calling minpac#add() tells minpac that you intend to install the plugin. To actually install the plugin, you have to call the minpac#update() function. For each plugin that you’ve registered, minpac will fetch the latest version from github.

First, source your vimrc file, then call the function:

:source $MYVIMRC
:call minpac#update()

You won’t see much output while the command runs. Minpac uses Vim’s job control API to fetch the plugins in the background. When finished, it prints a summary showing how many plugins were installed or updated.

Removing plugins

To uninstall a plugin, remove the line of code in your vimrc where you registered the plugin, then source your vimrc and call the minpac#clean() function.

:source $MYVIMRC
:call minpac#clean()

Minpac will loop through all of the plugins in the pack/minpac/{start,opt} directories, marking for deletion any plugins that are not registered with minpac. You will see a prompt naming each plugin to be deleted, which gives you the chance to opt out if you change your mind.

Convenience commands

As the name suggests, minpac is a minimal package manager. It doesn’t define any commands it only defines functions, which you can execute via the :call command. For ease of use, you can create your own commands:

command! PackUpdate call minpac#update()
command! PackClean call minpac#clean()

Instead of :call minpac#update() you can now run :PackUpdate to do the same thing.

How I use minpac

I like to specify my plugins in a separate file called packages.vim, which starts like this:

command! PackUpdate packadd minpac | source $MYVIMRC | redraw | call minpac#update()
command! PackClean  packadd minpac | source $MYVIMRC | call minpac#clean()

if !exists('*minpac#init')
  finish
endif

call minpac#init()

call minpac#add('k-takata/minpac', {'type': 'opt'})
call minpac#add('tpope/surround')
" ...

My vimrc file contains this line, which loads the packages.vim file:

source ~/.vim/packages.vim

This is a variation of the method described in the documentation for loading minpac on demand. With this technique, minpac doesn’t load when Vim starts up. It only loads when you call either :PackUpdate or :PackClean. Pretty cool!

Further reading

Here’s an implementation detail which I find interesting: minpac uses Prabir Shrestha’s async.vim to run the update/clean jobs. The job control API differs between Neovim and Vim 8, which is a pain if you want to write plugins that work in both. The async.vim plugin normalizes these APIs, taking out the hassle.

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..