Skip to content

Instantly share code, notes, and snippets.

@shanehull
Last active February 1, 2026 00:16
Show Gist options
  • Select an option

  • Save shanehull/adaf46dddf2b13e147ae916adc9fb9e6 to your computer and use it in GitHub Desktop.

Select an option

Save shanehull/adaf46dddf2b13e147ae916adc9fb9e6 to your computer and use it in GitHub Desktop.
NeoVim Zettelkasten Command
-- **User command to create a new zettelkasten style note in 0-inbox** --
vim.api.nvim_create_user_command("Zet", function()
local root = os.getenv("SECOND_BRAIN") or (os.getenv("HOME") .. "/secondbrain")
local directory = root .. "/0-inbox"
local status, title = pcall(function()
return vim.fn.input("Note Title: ")
end)
if not status or title == "" then
print("Aborted.")
return
end
local slug = title:lower()
slug = slug:gsub("[^a-z0-9%s-]", "")
slug = slug:gsub("%s+", "-")
slug = slug:gsub("-+", "-")
slug = slug:gsub("^%-+", ""):gsub("%-+$", "")
if slug == "" then
print("Error: Title generated an empty ID.")
return
end
local fpath = directory .. "/" .. slug .. ".md"
local f_check = io.open(fpath, "r")
if f_check then
f_check:close()
print("Error: Note already exists with ID: " .. slug)
return
end
local current_date = os.date("%Y-%m-%d")
local template = [[
---
id: %s
aliases: []
tags:
- change-me
date: "%s"
---
# %s
]]
local file_content = string.format(template, slug, current_date, title)
local file = io.open(fpath, "w")
if file then
file:write(file_content)
file:close()
print("File created: " .. slug)
vim.api.nvim_command("edit " .. fpath)
vim.api.nvim_command("normal G")
vim.api.nvim_command("startinsert")
else
error("Error creating file: " .. fpath)
end
end, { desc = "Create a new zettelkasten note from Title" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment