Last active
May 31, 2024 16:09
-
-
Save Palanikannan1437/e78c1b7e5daa8390ef92e53ce8dba9d9 to your computer and use it in GitHub Desktop.
A simple lazy snippet to quickly surround selected code with triple backticks in neovim to paste in ChatGPT ;)
This file contains 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
local function surround_selection_with_backticks() | |
-- Get the visual selection | |
local lines = get_visual_selection() | |
-- Ensure lines are not nil | |
if not lines or #lines == 0 then | |
print("No lines selected") | |
return | |
end | |
-- Surround the text with triple dots | |
table.insert(lines, 1, "```") | |
table.insert(lines, "```") | |
-- Join the lines into a single string | |
local text = table.concat(lines, "\n") | |
-- Copy the text to the system clipboard | |
vim.fn.setreg("+", text) | |
print("Copied to clipboard with surrounding dots") | |
end | |
local function get_visual_selection() | |
local _, ls, cs, _ = unpack(vim.fn.getpos("v")) | |
local _, le, ce, _ = unpack(vim.fn.getpos(".")) | |
-- Swap if the selection is made from bottom to top | |
if ls > le or (ls == le and cs > ce) then | |
ls, le = le, ls | |
cs, ce = ce, cs | |
end | |
return vim.api.nvim_buf_get_lines(0, ls - 1, le, false) | |
end | |
-- Map it to a key to run the above function post your visual selection! | |
vim.keymap.set("v", "<leader>c", surround_selection_with_backticks, { desc = "Surround selection with backticks" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment