all repos — nvim-custom-links @ 9e46ec3454b9053923ac6751c7cb674eef8fe2b8

Neovim simple plugin for custom local links

Setup first plugin version
Tim Izzo tim@octree.ch
Sat, 05 Apr 2025 21:58:07 +0200
commit

9e46ec3454b9053923ac6751c7cb674eef8fe2b8

2 files changed, 74 insertions(+), 0 deletions(-)

jump to
A README.md

@@ -0,0 +1,24 @@

+# NVIM Custom Links + +Set words as links with custom path un Neovim. + +## LazyVim + +```lua +return { + { + "https://git.5ika.ch/nvim-custom-links.git", + config = function() + vim.cmd([[ + let g:custom_links_project_base_path = '~/projects' + let g:custom_links_area_base_path = '~/areas' + let g:project_template_path = '~/templates/project.md' + let g:area_template_path = '~/templates/area.md' + ]]) + end, + }, +} +``` + + +```
A plugin/custom_links.lua

@@ -0,0 +1,50 @@

+-- Default paths for custom links +local project_base_path = vim.fn.expand("~/5ika/brain/project") +local area_base_path = vim.fn.expand("~/5ika/brain/area") + +-- Template paths +local project_template_path = vim.fn.expand("~/5ika/brain/templates/project.md") +local area_template_path = vim.fn.expand("~/5ika/brain/templates/area.md") + +-- Configurable prefixes for custom links +local link_prefixes = { + project = "+", + area = "@", +} + +-- Function to create a file from a template +local function create_file_from_template(full_path, template_path) + local template_content = vim.fn.readfile(template_path) + vim.fn.writefile(template_content, full_path) + print("Created new file from template: " .. full_path) +end + +-- Function to follow custom links +function follow_custom_link() + local word = vim.fn.expand("<cWORD>") + + local full_path, template_path + + if string.match(word, "^" .. link_prefixes.project .. "%w+$") then + local project_name = string.gsub(word, "^" .. link_prefixes.project, "") + full_path = project_base_path .. "/" .. project_name .. ".md" + template_path = project_template_path + elseif string.match(word, "^" .. link_prefixes.area .. "%w+$") then + local area_name = string.gsub(word, "^" .. link_prefixes.area, "") + full_path = area_base_path .. "/" .. area_name .. ".md" + template_path = area_template_path + else + print("Not a custom link") + return + end + + if vim.fn.filereadable(full_path) == 1 or vim.fn.isdirectory(full_path) == 1 then + vim.cmd("edit " .. full_path) + else + create_file_from_template(full_path, template_path) + vim.cmd("edit " .. full_path) + end +end + +-- Key mapping to follow custom links +vim.api.nvim_set_keymap("n", "<leader>fl", ":lua follow_custom_link()<CR>", { noremap = true, silent = true })