Packaging a Plugin
Structure and share it.
Packaging a Plugin is a free Lua Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Plugin Directory Layout
A Neovim plugin is just a directory on the runtimepath. The conventional layout has top-level folders that Neovim treats specially.
Key directories: lua/ for modules, plugin/ for auto-loaded setup, ftplugin/ for filetype scripts, doc/ for help, and after/ for late overrides.
-- myplugin/
-- lua/myplugin/init.lua
-- plugin/myplugin.lua
-- doc/myplugin.txtThe lua/ Directory
Files under lua/ are reachable with require. A module at lua/myplugin/init.lua loads as require('myplugin').
Nesting maps to dotted paths: lua/myplugin/config.lua becomes require('myplugin.config'). This is how plugins expose a clean public namespace.
-- in lua/myplugin/init.lua
local M = {}
function M.hello() print('hi') end
return MReturning a Module Table
The idiomatic module pattern declares a local table M, attaches functions, and returns it. Callers then access require('myplugin').hello().
Keep internal helpers as plain locals so only the intended surface is public. This mirrors module encapsulation in other ecosystems.
local M = {}
local function private() end
function M.run() private() end
return MThe setup() Convention
Most plugins expose a setup(opts) function. It merges user options over defaults and performs initialization like creating commands and autocommands.
Use vim.tbl_deep_extend('force', defaults, opts or {}) so partial user config still gets all defaults.
local M = {}
local defaults = { width = 40 }
function M.setup(opts)
M.config = vim.tbl_deep_extend('force', defaults, opts or {})
end
return MThe plugin/ Directory
Scripts in plugin/ run automatically when Neovim starts, after runtimepath is built. Keep them tiny.
A common job is registering commands or a guard so heavy modules load lazily. Avoid expensive work here; defer it to setup or autocommands to keep startup fast.
-- in plugin/myplugin.lua
if vim.g.loaded_myplugin then return end
vim.g.loaded_myplugin = trueLoad Guards
A load guard prevents double initialization if the file is sourced twice. Set a vim.g.loaded_* flag and bail early on re-entry.
This is essential because plugin managers and :runtime can re-source files, and duplicate commands or autocommands cause subtle bugs.
if vim.g.loaded_myplugin == 1 then return end
vim.g.loaded_myplugin = 1Lazy Loading
Fast startup means loading code only when needed. Register a lightweight command in plugin/ that requires the heavy module on first use.
Plugin managers like lazy.nvim formalize this with cmd, ft, and keys triggers, so your module is not touched until invoked.
vim.api.nvim_create_user_command('MyStart', function()
require('myplugin').run()
end, {})runtimepath and packpath
Neovim discovers plugins by scanning runtimepath. The native package system loads directories under pack/*/start/ automatically and pack/*/opt/ on demand via :packadd.
Most users rely on a manager, but understanding runtimepath explains how your folders are found.
print(vim.o.runtimepath:sub(1, 60))
-- :packadd loads an opt plugin manuallyHealth Checks
Provide a lua/myplugin/health.lua with a check function so users can run :checkhealth myplugin. Report status with the vim.health API.
Use vim.health.start, vim.health.ok, vim.health.warn, and vim.health.error to surface missing dependencies clearly.
local M = {}
function M.check()
vim.health.start('myplugin')
vim.health.ok('all good')
end
return MDocumentation and Tags
Ship a doc/myplugin.txt help file. Run :helptags doc/ (or let the manager do it) to generate the tags index so :help myplugin works.
Good docs list commands, the setup options, and default keymaps, making your plugin discoverable from inside Neovim.
-- generate tags from the doc directory
vim.cmd('helptags ' .. vim.fn.expand('%:p:h'))Versioning and Publishing
Host the plugin in a git repository; users install it by the owner/repo path. Tag releases semantically so managers can pin versions.
Include a README with install snippets for popular managers, a license, and a minimal config example to lower the barrier to adoption.
-- lazy.nvim spec
-- { 'owner/myplugin', config = function()
-- require('myplugin').setup({})
-- end }Quick Check
Confirm your understanding of plugin packaging.
Recap: Packaging a Plugin
A plugin is a runtimepath directory with lua/ modules, an auto-run plugin/ script, and optional doc/, ftplugin/, and health files.
Return module tables, expose a setup that merges defaults, guard against double-loading, lazy-load heavy code, document with helptags, and publish via git with semantic version tags.
Frequently asked questions
Is the “Packaging a Plugin” lesson free?
Yes — the full text of “Packaging a Plugin” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Packaging a Plugin”?
Structure and share it. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Packaging a Plugin” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Neovim Lua API
- Commands and Keymaps
- Buffers and Windows
- Packaging a Plugin