-
Copilot Chat typically replies with markdown code blocks, ```py I find myself often copying the code from the copilot chat buffer to another buffer. What's the easiest way to copy only the code? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
I ended up using something suggested by ChatGPT. If anyone knows something better, I'm still interested. -- identifies code between ``` and yanks it
function YankCodeBlock()
-- Find the start of the code block
local start_line = vim.fn.search("^```", "bnW")
if start_line == 0 then
return
end
-- Find the end of the code block
local end_line = vim.fn.search("^```", "nW")
if end_line == 0 then
return
end
-- Adjust start line to skip the language identifier
start_line = start_line + 1
-- Yank the content
vim.cmd(string.format("%d,%dy", start_line, end_line - 1))
print("Code block yanked")
end
-- Set the mapping
vim.api.nvim_set_keymap("n", "<leader>yc", [[:lua YankCodeBlock()<CR>:echo<CR>]], { noremap = true, silent = true }) |
Beta Was this translation helpful? Give feedback.
-
You can do this with the "fenced code block" text object, courtesty of nvim-various-textobjs. It allows you to press {
'chrisgrieser/nvim-various-textobjs',
lazy = false,
config = function()
-- https://github.com/chrisgrieser/nvim-various-textobjs#list-of-text-objects
require('various-textobjs').setup({
useDefaultKeymaps = false,
})
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup('markdown_keymaps', { clear = true }),
pattern = { "markdown", "copilot-chat" },
callback = function()
vim.keymap.set({ "o", "x" }, "aC", '<cmd>lua require("various-textobjs").mdFencedCodeBlock(false)<CR>', { desc = "Around Fenced Code Block" })
vim.keymap.set({ "o", "x" }, "iC", '<cmd>lua require("various-textobjs").mdFencedCodeBlock(true)<CR>', { desc = "Inside Fenced Code Block" })
end,
})
end,
} |
Beta Was this translation helpful? Give feedback.
-
I don't thave this setup but using |
Beta Was this translation helpful? Give feedback.
-
Thanks for both of the suggestions! I'm already using flash.nvim. To make the flash.nvim work for the copilotchat buffer, I first had to tell treesitter to treat it as markdown. So I added this to my autoload.lua: vim.api.nvim_create_autocmd("FileType", {
pattern = "copilot-chat",
callback = function()
vim.bo.filetype = "markdown"
end,
}) |
Beta Was this translation helpful? Give feedback.
I don't thave this setup but using
flash.nvim
to quickly select the codeblock for me.