π₯ Hatch.nvim is a Neovim plugin that automatically applies file templates to empty buffers. This plugin helps you start coding faster with pre-defined boilerplate code for different file types.
- Automatically inserts templates for empty buffers based on file extension.
- Supports custom templates, or default templates .
- Supports sharing templates from custom repositories, or pull them from the default repository
- Handles cursor placement via a
#cursor row:coldirective, placing your cursor right where you want it to be, so you can start coding immediately.
Using lazy.nvim
Minimal setup:
{
"codevogel/hatch.nvim",
opts = {},
}
Now optionally run :HatchCloneTemplates to clone the default template
repository.
- Run
:Hatchto hatch from a template file when in an empty buffer.- βΉοΈ This is done automatically on
BufWritePrewhen saving buffers whencreate_autocmd = true(on by default).
- βΉοΈ This is done automatically on
- Template files are selected based on file extension from the
template_directory(default:$HOME/.config/hatch.nvim/templates) - Templates can also be forced to overwrite a non-empty buffer using
:HatchForce
Templates are read from either {template_directory}/custom/{file_exension} or
{template_directory}/default/{file_exension}, where the former takes
precedence.
On the first line of a template file, you can add a cursor directive using
#cursor: row:col. This makes it easy to get your cursor to the exact place you
start writing code after hatching the template.
e.g.: editing the template.nix file, add #cursor: 4:1 to make your cursor
jump to line 4 (1-indexed), column 2 (0-indexed).
To make this process easy, just add the
#cursorline, then place your cursor and note down your row:col marker from the bottom right corner ofnvim, and subtract one to both numbers you see there. (subtracting 1 from the row because hatch will remove the#cursorline, and subtracting 1 from the column because they are 0-indexed innvim).
You can set the template_repository to your own fork of the
default template repository.
This just makes HatchCloneTemplates use that URL instead.
You can also just opt to clone a repository anywhere on your machine, and then
set template_directory to that path.
Create a file {template_directory}/default/template.gd or
{template_directory}/custom/template.gd:
#cursor 1:11
class_name Foo
function _ready() -> void:
pass
function process(delta: float) -> void:
passNow any empty buffer that ends in .gd will be replaced with above contents,
placing the cursor at the start of Foo, so you can immediately cw to update
it.
Sometimes it might be useful to make hatch run after the formatter, as your formatter might remove whitespace that you'd like to be preserved in your template.
In this case, just set create_autocmd = false and then run hatch after your
formatter.
For example, for conform.nvim we can just update the autocmd which formats the buffer to run hatch afterwards:
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
callback = function(args)
require("conform").format({
-- formatting options...
})
require("hatch").hatch()
end,
})These settings can be supplied to the setup function of the plugin in a table.
| Option | Type | Default | Description |
|---|---|---|---|
template_repository |
string | "git@github.com:codevogel/hatch.nvim-templates.git" |
Git repository containing default templates |
template_directory |
string | "$HOME/.config/hatch.nvim/templates" |
Local directory to store templates |
create_autocmd |
boolean | true |
Automatically apply templates on buffer write |
| Command | Description |
|---|---|
:Hatch |
Apply a template to the current buffer, overwriting its contents if empty. |
:HatchForce |
Force-apply the template to the current buffer, overwriting its contents. |
:HatchCloneTemplates |
Clone the template repository into your local template directory. |
~/.config/hatch.nvim/templates/
βββ default/ # Default templates from the repo
β βββ template.py # Python template
β βββ template.lua # Lua template
βββ custom/ # User-defined custom templates
βββ template.js # JavaScript template
- Custom templates override default templates when both exist.
- Templates are matched based on file extension.
