feat: add custom key mappings for visual mode text block movements

This commit is contained in:
2024-09-14 17:04:57 -04:00
parent 5615d74659
commit 0affef0abc
3 changed files with 84 additions and 36 deletions
+25
View File
@@ -455,6 +455,8 @@ function M.move_visual(up, multiplier)
vim.cmd("norm gv")
end
---@function Function to paste over text with overwrite
---@return nil
function M.paste_overwrite()
-- Get the register contents and type
local register = vim.fn.getreginfo(vim.v.register or '"')
@@ -490,6 +492,29 @@ function M.paste_overwrite()
)
end
---@function Function to dump the current buffer to a register and format as a Lua table
---@param register string? The name of the register to dump the buffer to (defaults to unnamed register if nil)
---@return nil
function M.dump_buffer_to_table(register)
register = register or "" -- Default to unnamed register if not provided
-- Get the lines from the current buffer
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
-- Process the buffer contents
local result = {}
for _, line in ipairs(lines) do
local entry = line:gsub("%.lua$", "") -- Remove ".lua" extension
table.insert(result, '"' .. entry .. '"')
end
-- Join the result into a single string and format as a Lua table
local output = "{ " .. table.concat(result, ", ") .. " }"
-- Dump the result into a register
vim.fn.setreg(register, output)
end
---@function Function to reload the user's Neovim configuration
---@return nil
function M.reload_config()
+53
View File
@@ -440,6 +440,59 @@ M.misc = {
mode = "n",
hidden = true,
},
{ -- Visual mode: Move selected block of text up
lhs = "K",
rhs = function()
require("data.func").move_visual(true)
end,
desc = "Move block of text up",
mode = "v",
},
{ -- Visual mode: Move selected block of text down
lhs = "J",
rhs = function()
require("data.func").move_visual(false)
end,
desc = "Move block of text down",
mode = "v",
},
{ -- Test Prompt: Enter your name
lhs = "<leader>qP",
rhs = function()
require("data.func").InputPrompt("Enter your name: ", function(input)
if input then
-- trim whitespace from end of input
input = input:gsub("%s+$", "")
print("👋😎 Hello " .. input .. "!")
else
print("Input was canceled")
end
end)
end,
desc = "Test Prompt",
},
{ -- Paste over text with overwrite
lhs = "<leader>cp",
rhs = function()
require("data.func").paste_overwrite()
end,
desc = "Paste overwrite",
mode = "n",
},
{ -- Dump buffer contents to a table in register
lhs = "<leader>bY",
rhs = function()
require("data.func").dump_buffer_to_table("+")
end,
desc = "Yank buffer as table",
mode = "n",
},
{ -- Yank buffer
lhs = "<leader>by",
rhs = "<cmd>%y<cr>",
desc = "Yank buffer",
mode = "n",
},
}
M.nekifoch = {