Created
April 6, 2025 03:54
-
-
Save eduardoarandah/699063a411a72a81fcd1de8508860590 to your computer and use it in GitHub Desktop.
neovim command to copy selection as a code snippet, with filename, line number and then the code snippet
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 copy selection as a code snippet, with filename, line number and then the code snippet | |
vim.api.nvim_create_user_command("CopyCodeSnippet", function(opts) | |
local start_line = vim.fn.line("'<") - 1 | |
local end_line = vim.fn.line("'>") | |
local file_name = vim.fn.expand("%:.") | |
local lines = vim.api.nvim_buf_get_lines(0, start_line, end_line, false) | |
local file_type = vim.api.nvim_buf_get_option(0, "filetype") | |
-- set clipboard | |
vim.fn.setreg("*", file_name .. " +" .. (start_line + 1) .. "\n\n```" .. file_type .. "\n" .. table.concat(lines, "\n") .. "\n```") | |
vim.notify("Added code snippet to clipboard", vim.log.levels.INFO) | |
end, { range = true, bang = true }) | |
-- map it to F6 on visual mode | |
vim.api.nvim_set_keymap("v", "<F6>", ":CopyCodeSnippet<CR>", { noremap = true }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment