From 4ce7e9201c53564a765ad9c537e0898a8ad959f0 Mon Sep 17 00:00:00 2001 From: Nami Lauckner Date: Sun, 2 Aug 2026 11:24:33 +0200 Subject: [PATCH] Fixed swapwords, Added goated helper --- after/plugin/lsp.lua | 4 +- after/plugin/presense.lua | 2 +- lua/nami/remap.lua | 84 ++++++++++++++++++++++++++++++--------- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/after/plugin/lsp.lua b/after/plugin/lsp.lua index 8606b76..771b17e 100644 --- a/after/plugin/lsp.lua +++ b/after/plugin/lsp.lua @@ -43,9 +43,9 @@ vim.api.nvim_create_autocmd("LspAttach", { local opts = { buffer = args.buf } vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) -- Show type and defintion data (C-w C-w to enter it like any other buffer) - vim.keymap.set("n", "d", vim.diagnostic.open_float, opts) -- Show a detailed error description (can be run on lines with any error) + vim.keymap.set("n", "e", vim.diagnostic.open_float, opts) -- Show a detailed error description (can be run on lines with any error) vim.keymap.set("n", "gd", builtin.lsp_definitions, opts) -- Go to definition - vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) -- Rename symbol under cursor project wide through ls + vim.keymap.set("n", "vrn", vim.lsp.buf.rename, opts) -- Rename symbol under cursor project wide through ls vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) -- Execute a ls code action -- vim.keymap.set("n", "[d", function() vim.diagnostic.jump({ count = 1, float = true }) end, opts) diff --git a/after/plugin/presense.lua b/after/plugin/presense.lua index fc1bf59..1e395ff 100644 --- a/after/plugin/presense.lua +++ b/after/plugin/presense.lua @@ -37,4 +37,4 @@ function ToggleDiscordPresence() end -- Map it to a key (e.g., dp for "Discord Presence") -vim.keymap.set("n", "dp", ToggleDiscordPresence, { desc = "Toggle Discord Presence" }) +vim.keymap.set("n", "tdp", ToggleDiscordPresence, { desc = "Toggle Discord Presence" }) diff --git a/lua/nami/remap.lua b/lua/nami/remap.lua index 3f015d4..238d7e1 100644 --- a/lua/nami/remap.lua +++ b/lua/nami/remap.lua @@ -45,7 +45,7 @@ end vim.keymap.set("v", "J", ":m '>+1gv=gv") -- Move selected text up one line vim.keymap.set("v", "K", ":m '<-2gv=gv") -- Move selected text down one line -vim.keymap.set("n", "J", "mzJ`z") -- Remove newline characte on current line +vim.keymap.set("n", "J", "mzJ`z") -- Remove newline character on current line vim.keymap.set("n", "f", function() -- Format buffer vim.lsp.buf.format() @@ -61,7 +61,8 @@ vim.keymap.set("v", "y", "\"+y") vim.keymap.set("n", "Y", "\"+Y") -- Delete to void register (no clipboard) vim.keymap.set("v", "d", "\"_d") -vim.keymap.set("v", "dd", "\"_d") +vim.keymap.set("n", "d", "\"_d") +vim.keymap.set("n", "c", "\"_c") -- currently conflicting with searches vim.keymap.set("x", "P", "\"_dP") -- Paste before without moving selection into the yank register vim.keymap.set("x", "p", "\"_dp") -- Paste after without moving selection into the yank register @@ -69,14 +70,7 @@ vim.keymap.set("x", "p", "\"_dp") -- Paste after without moving selectio ------------ -- Macros -- ------------ -vim.keymap.set("n", "c", ":w:!cargorun") -- Quick Cargo run - -- format a markdown table inside visual selection -vim.api.nvim_create_user_command("Mdtf", function(opts) - vim.cmd(string.format("%d,%d!column -t -s '|' -o '|'", opts.line1, opts.line2)) -end, { - range = true, -}) vim.keymap.set("v", "mt", ":!column -t -s '|' -o '|'", { desc = "Format markdown table" }) -- Scripting @@ -87,12 +81,54 @@ vim.keymap.set("n", "l", "source %") -- source the curren vim.keymap.set("n", "l", ":.lua") -- source the current line vim.keymap.set("v", "l", ":lua") -- source the visual selection --- Swap all occurances of two sequences with each other inside a file --- This may be a bit buggy still -local function swap_words() - local start = vim.fn.getpos("'<")[2] - local finish = vim.fn.getpos("'>")[2] +-- Places content on current line at the back of next line +-- This: +-- // returns +-- return +-- Turns into this: +-- return // retuns +vim.keymap.set("n", "L", "0d$jA pk\"_dd$") +-- Does the same on a selection of lines +-- This: +-- // returns +-- // something +-- return +-- Turns into this: +-- return // returns +-- // something +vim.keymap.set("v", "L", ":m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0") +-- How it works: +-- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 +-- ^ +-- move selection down one line +-- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 +-- ^ +-- put cursor at start of line selection +-- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 +-- ^ +-- go back to visual mode +-- anchor selection to bottom left and top right +-- go to visual block mode +-- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 +-- ^ +-- delete the block +-- delete one line to void +-- add block to the back of the line +-- Moves the cursor to the start of the current selection +function SelectionStart() + local v = vim.fn.getpos("v") + local c = vim.fn.getpos(".") + + if v[2] < c[2] then + vim.api.nvim_input("o") + end +end + +-- Swap all occurances of two sequences with each +--- @param start number line of chunk +--- @param end_ number line of chunk +function SwapWords(start, end_) local w1 = vim.fn.input("Word 1: ") local w2 = vim.fn.input("Word 2: ") @@ -100,7 +136,7 @@ local function swap_words() return end - local lines = vim.api.nvim_buf_get_lines(0, start - 1, finish, false) + local lines = vim.api.nvim_buf_get_lines(0, start - 1, end_, false) for i, line in ipairs(lines) do line = line:gsub(vim.pesc(w1), "__TMP__") @@ -109,8 +145,20 @@ local function swap_words() lines[i] = line end - vim.api.nvim_buf_set_lines(0, start - 1, finish, false, lines) + vim.api.nvim_buf_set_lines(0, start - 1, end_, false, lines) end -vim.keymap.set("v", "ws", swap_words, { desc = "Swap two words in selection" }) -vim.keymap.set("n", "ws", swap_words, { desc = "Swap two words in selection" }) +-- Swap two words in current selection +vim.keymap.set("v", "ws", function() + local start = vim.fn.getpos("v")[2] + local end_ = vim.fn.getpos(".")[2] + + SwapWords(math.min(start, end_), math.max(start, end_)) +end, { desc = "Swap two words in selection" }) +-- Swap two words in previous selection +vim.keymap.set("n", "ws", function() + local start = vim.fn.getpos("'<")[2] + local end_ = vim.fn.getpos("'>")[2] + + SwapWords(math.min(start, end_), math.max(start, end_)) +end, { desc = "Swap two words in previous selection" })