Last active
April 10, 2024 11:25
-
-
Save shanehull/adaf46dddf2b13e147ae916adc9fb9e6 to your computer and use it in GitHub Desktop.
NeoVim Zettelkasten Command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Command to create a new zettelkasten style note in 0-inbox of your second brain | |
vim.api.nvim_create_user_command("Zet", function() | |
local directory = os.getenv("SECOND_BRAIN") .. "/0-inbox" or os.getenv("HOME") .. "secondbrain/0-inbox/" | |
-- Get the file name from the user | |
local status, result = pcall(function() | |
Fname = vim.fn.input("File name: ", "", "file") | |
end) | |
-- Handle error (e.g. keyboard interrupt) | |
if not status then | |
print("Aborted:", result) | |
return | |
end | |
local fpath = directory .. "/" .. Fname .. ".md" | |
local current_date = os.date("%Y-%m-%d") | |
local title = Fname:gsub("%-", " "):gsub("(%w)(%w*)", function(first, rest) | |
return first:upper() .. rest:lower() | |
end) | |
local template = [[ | |
--- | |
id: %s | |
aliases: | |
tags: | |
- change-me | |
date: "%s" | |
--- | |
# %s | |
]] | |
local file_content = string.format(template, Fname, current_date, title) | |
-- Write the content to the file | |
local file = io.open(fpath, "w") | |
if file then | |
file:write(file_content) | |
file:close() | |
print("File created: " .. fpath) | |
-- Open the file for writing | |
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 style note" }) | |
-- Add a keymap to run the Zet command for easy access | |
vim.api.nvim_set_keymap("n", "<leader>Z", ":Zet<CR>", { desc = "New zettelkasten note" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment