From 7cda320f8ca1ad027f59c583edeeefcd4c7d0d08 Mon Sep 17 00:00:00 2001 From: rootiest Date: Sat, 14 Sep 2024 15:21:49 -0400 Subject: [PATCH] feat: add key mapping for paste overwrite --- lua/config/keymaps.lua | 5 +++++ lua/data/func.lua | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 1b0f11c..a6bd586 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -73,3 +73,8 @@ add_keymap("qP", function() end end) end, "Test Prompt") + +-- Paste over text with overwrite +add_keymap("cp", function() + data.func.paste_overwrite() +end, "Paste overwrite", "n") diff --git a/lua/data/func.lua b/lua/data/func.lua index 439bb97..28ab846 100644 --- a/lua/data/func.lua +++ b/lua/data/func.lua @@ -455,6 +455,41 @@ function M.move_visual(up, multiplier) vim.cmd("norm gv") end +function M.paste_overwrite() + -- Get the register contents and type + local register = vim.fn.getreginfo(vim.v.register or '"') + local regcontents = register.regcontents + + -- Enter Virtual Replace Mode + vim.api.nvim_feedkeys("gR", "n", false) + + -- Process each line in the register contents + for i, line in ipairs(regcontents) do + if i > 1 then + -- Handle formatting of multi-line pastes (except for the first line) + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("0gR", true, false, true), + "n", + false + ) + end + + -- Paste the current line; add a newline if it's not the last line + if i < #regcontents then + vim.api.nvim_feedkeys(line .. "\n", "n", false) + else + vim.api.nvim_feedkeys(line, "n", false) + end + end + + -- Properly exit Virtual Replace Mode using + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("", true, false, true), + "n", + false + ) +end + ---@function Function to reload the user's Neovim configuration ---@return nil function M.reload_config()