Fixed swapwords, Added goated helper

This commit is contained in:
2026-08-02 11:24:33 +02:00
parent 51c420a20c
commit 4ce7e9201c
3 changed files with 69 additions and 21 deletions
+66 -18
View File
@@ -45,7 +45,7 @@ end
vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv") -- Move selected text up one line
vim.keymap.set("v", "K", ":m '<-2<CR>gv=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", "<leader>f", function() -- Format buffer
vim.lsp.buf.format()
@@ -61,7 +61,8 @@ vim.keymap.set("v", "<leader>y", "\"+y")
vim.keymap.set("n", "<leader>Y", "\"+Y")
-- Delete to void register (no clipboard)
vim.keymap.set("v", "<leader>d", "\"_d")
vim.keymap.set("v", "<leader>dd", "\"_d")
vim.keymap.set("n", "<leader>d", "\"_d")
vim.keymap.set("n", "<leader>c", "\"_c")
-- currently conflicting with searches
vim.keymap.set("x", "<leader>P", "\"_dP") -- Paste before without moving selection into the yank register
vim.keymap.set("x", "<leader>p", "\"_dp") -- Paste after without moving selection into the yank register
@@ -69,14 +70,7 @@ vim.keymap.set("x", "<leader>p", "\"_dp") -- Paste after without moving selectio
------------
-- Macros --
------------
vim.keymap.set("n", "<leader>c", ":w<CR>:!cargo<Space>run<CR>") -- 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", "<leader>mt", ":!column -t -s '|' -o '|'<CR>", { desc = "Format markdown table" })
-- Scripting
@@ -87,12 +81,54 @@ vim.keymap.set("n", "<space><space>l", "<cmd>source %<CR>") -- source the curren
vim.keymap.set("n", "<space>l", ":.lua<CR>") -- source the current line
vim.keymap.set("v", "<space>l", ":lua<CR>") -- 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 '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0")
-- How it works:
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0
-- ^
-- move selection down one line
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0
-- ^
-- put cursor at start of line selection
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0
-- ^
-- go back to visual mode
-- anchor selection to bottom left and top right
-- go to visual block mode
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>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", "<leader>ws", swap_words, { desc = "Swap two words in selection" })
vim.keymap.set("n", "<leader>ws", swap_words, { desc = "Swap two words in selection" })
-- Swap two words in current selection
vim.keymap.set("v", "<leader>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", "<leader>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" })