Plugins
A plugin is code someone else wrote that your editor loads at startup and runs as part of itself — new commands, new behaviour, new interfaces. This chapter explains what a plugin physically is, installs one by hand with no tooling at all (a five-line plugin the lab wrote for the purpose), and then uses the manager Neovim now builds in. It also repeats the rule from chapter 1, at the moment it matters:
A plugin runs with your full user powers. It can read your files, run your shell, reach your network, because you can. That is not a flaw in the design — it is what makes plugins useful — but it means installing one is running a program, and it deserves the same half-minute of thought: a project you can name, a version you chose.
What a plugin physically is
Strip the tooling away and a plugin is
a folder of files placed where the
editor looks. Neovim's startup searches
its runtimepath — the list of
directories it loads code from — and
runs the files in any plugin/ folder
it finds there. The classical packaging
for that is a package: under the
data folder, site/pack/<group>/start/<plugin>/
is loaded automatically at startup
(and opt/ instead of start/ means
load only when asked, with
:packadd).
So installing a plugin by hand is
copying a folder into start/. The lab
proved the whole mechanism with a
plugin too small to hide anything in
(kept at neovim-lab/plugin/labgreet.lua):
-- labgreet: a five-line plugin the lab wrote itself.
-- It adds one command, :LabGreet, which stamps a greeting line
-- at the top of the current buffer.
if vim.g.loaded_labgreet then
return
end
vim.g.loaded_labgreet = true
vim.api.nvim_create_user_command('LabGreet', function()
local name = vim.fn.input('Name: ')
vim.api.nvim_buf_set_lines(0, 0, 0, false, { 'Hello, ' .. name .. '! (added by labgreet)' })
end, { desc = 'Insert a greeting at the top of the buffer' })
Copied to
stdpath('data')/site/pack/lab/start/labgreet/plugin/labgreet.lua,
it needed no registration and no
restart ritual beyond starting the
editor. The lab ran its command —
answering the name prompt with fed
keystrokes — and the file on disk
gained its first line (transcript
neovim-lab/transcripts/t09b-pack-plugin.txt):
Hello, reader! (added by labgreet)
first draft
Real plugins are this, at scale: more
files, a lua/ folder of modules, a
plugin/ file that wires them up.
When a plugin "won't load", the cause
is almost always placement — the folder
is not where the runtimepath looks —
and :set runtimepath? and
:scriptnames (which lists every file
the editor has loaded, in order) are
how you see the truth.
vim.pack: the built-in manager
Placing folders by hand is fine for
one plugin and tedious for twenty, so
the ecosystem grew plugin managers —
programs that fetch plugins from their
repositories, keep them in the right
folders, and update them on request.
Since Neovim 0.12 a small one is
built in: vim.pack. In your
init.lua:
vim.pack.add({
'https://github.com/echasnovski/mini.nvim',
})
starts the editor with that plugin
cloned, placed and loaded. The lab ran
exactly this against mini.nvim — a
respected collection of small modules
by a Neovim maintainer — then loaded
one of its modules, mini.comment, and
called its setup (transcript
neovim-lab/transcripts/t09-vim-pack.txt):
mini.comment required: true
installed by vim.pack: mini.nvim
vim.pack deliberately does less than
the big managers. It installs from git
repositories, updates on request
(vim.pack.update()), and records
what it fetched in a lockfile
(nvim-pack-lock.json, in the config
folder) so a setup can be reproduced.
It does not lazy-load plugins on
demand, run build steps for you, or
present an interface; the long-standing
third-party manager lazy.nvim does
all of those and is what most large
published configurations use. That
manager is described here from its
documentation — the lab ran vim.pack
and the by-hand route, and those are
the two the transcripts vouch for.
Knowing the by-hand route first is
what makes either manager legible:
both are, underneath, arranging
folders on the runtimepath.
The discipline that keeps plugins safe to use
Three habits, cheap and sufficient for
most people. Pin by updating
deliberately: run the update command
as an event, read what changed, and
keep the lockfile in version control
with the rest of your config, so an
update that breaks something can be
rolled back by checking out the old
lock. Install few: every plugin is
code you now maintain a relationship
with; the built-ins of chapters 7 and
8 cover ground that once needed five
plugins. Read the small print of
:checkhealth after a big install —
plugins register their own health
checks there, and it is where a
missing external tool (a compiler, a
search program) announces itself,
rather than as a mysterious failure
mid-edit.