Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Configuration and Plugins

Plugins and Packages

Install and manage Vim plugins with native packages (pack/*/start and opt), vim-plug, how runtimepath works, helptags, colorschemes, netrw and startup time.

A plugin is a folder of Vim script (or Lua, in Neovim) files placed where Vim looks for them. Vim has gone through several ways to manage that folder, and today there are two good ones: the built-in packages system and a plugin manager that downloads and updates things for you.

How Vim finds files: runtimepath

Everything Vim loads comes from directories in the option runtimepath (:set rtp?). Inside each directory it looks for standard subfolders:

Folder Contents
plugin/ Scripts loaded at startup
autoload/ Scripts loaded on demand when a function foo#bar() is called
ftplugin/ Per-filetype settings, e.g. ftplugin/python.vim
indent/ Indent rules per filetype
syntax/ Syntax highlighting
colors/ Colorschemes
doc/ Help files
after/ Scripts that must run after everything else (overrides)

A "plugin manager" mostly adds each plugin's folder to runtimepath.

flowchart TD
    A["Vim starts"] --> B["Read vimrc"]
    B --> C["Load packages in pack/*/start"]
    C --> D["Scan runtimepath: plugin/*.vim"]
    D --> E["Ready; opt packages load on :packadd"]

Startup order in short. Exact details are in :help startup.

Native packages

Vim 8 (and Neovim) load plugins from package directories with no manager at all. The layout is:

TEXT
~/.vim/pack/<anything>/start/<plugin>/     loaded automatically
~/.vim/pack/<anything>/opt/<plugin>/       loaded only with :packadd

(In Neovim the root is ~/.local/share/nvim/site/pack/ on Linux and macOS.)

To install a plugin by hand, clone it:

Terminal
git clone https://github.com/tpope/vim-commentary \
  ~/.vim/pack/plugins/start/vim-commentary

Then generate its help tags with :helptags ALL and restart. The name plugins is yours to choose. Put plugins you rarely need into opt/ and load them when wanted: :packadd cfilter. Vim ships a few optional packages itself, for example matchit (smarter %), cfilter and comment (in recent Vim versions): packadd! matchit in the vimrc enables the first.

Updating means running git pull in the folder. For a handful of plugins this is fine, and it keeps your setup simple.

vim-plug

When you have many plugins, a manager helps. A popular one is vim-plug, a single file plug.vim placed in the autoload folder. Your vimrc declares plugins between two calls:

VIM
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-surround'
Plug 'tpope/vim-commentary'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
call plug#end()

Run :PlugInstall to download, :PlugUpdate to update, :PlugClean to remove plugins no longer listed and :PlugStatus to check. The on option lazy-loads a plugin when its command is first used, and for loads it for file types. The manager's own documentation covers installing the file; Neovim users usually pick a Lua manager instead (see Neovim).

Alternatives exist (Vundle, Pathogen, dein, minpac), but they all do the same thing: place folders on the runtimepath.

JS Stand-in: a runtimepath and package loader

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Colorschemes and the built-in file explorer

:colorscheme (or :colo) lists and applies a theme; Vim ships several (desert, slate, industry, habamax, lunaperche...). A theme is just a file in a colors/ folder, so you install one like any plugin. Put colorscheme name in your vimrc after syntax on and set background.

Vim also has a file explorer called netrw, with no plugin required: :Explore (or :E) opens the current directory in the window, :Vexplore in a vertical split, :Sexplore in a horizontal one. Inside it, Enter opens, - goes up, % creates a file, d creates a directory, D deletes, R renames. Netrw can also read remote paths such as scp://user@host/path and https:// URLs. Many people replace it with a plugin, but it is always there.

Choosing plugins sensibly

Start with a short list and add one at a time. These categories cover most needs, and good plugins exist for each:

  • Commenting and surrounding: toggling comments, changing quotes and brackets around text.
  • Fuzzy finding: open files, buffers and search results quickly (fzf, Telescope in Neovim).
  • Git integration: status, blame, diffs (see the git lesson).
  • Statusline and tabline: lightweight bars (lightline, airline).
  • Language support: syntax, linting and completion. In Neovim use built-in LSP.

Before adding any plugin, ask whether Vim already does it. Many plugins replace features that exist (:help is searchable: :helpgrep word), and each plugin costs startup time and may conflict with your mappings.

Measuring startup time

vim --startuptime startup.log writes a list of everything loaded and how long it took. In Neovim use nvim --startuptime. Sort the log by the time column to find the slow plugin, then lazy-load it or remove it.

JS Stand-in: find the slowest startup item

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do you put a plugin so Vim loads it automatically with native packages?

    Plugins in pack//start load at startup; those in pack//opt wait for :packadd.
  2. 2. What does :PlugInstall do?

    :PlugInstall belongs to vim-plug. :PlugClean removes unlisted plugins.
  3. 3. What is runtimepath?

    Plugin managers work by adding directories to runtimepath.
  4. 4. How do you find which plugin slows Vim down at start?

    --startuptime writes timings for every sourced script.

Summary

  • Vim loads files from the directories in runtimepath; plugins add themselves there.
  • Native packages: pack/*/start loads automatically, pack/*/opt loads with :packadd.
  • vim-plug (or a Lua manager in Neovim) downloads and updates plugins from a list.
  • Vim ships netrw, colorschemes and optional packages such as matchit.
  • Add plugins slowly and measure with --startuptime.

Next: Neovim: What Is Different