From d449e0a73a970c128635b375617211a7232865cd Mon Sep 17 00:00:00 2001 From: rizkyilhampra Date: Sun, 28 Jan 2024 17:26:54 +0800 Subject: [PATCH] chore(Keymaps): refactor and add duplicate selection keymap --- lua/aquila/core/keymaps.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lua/aquila/core/keymaps.lua b/lua/aquila/core/keymaps.lua index c7bbae6..826e720 100644 --- a/lua/aquila/core/keymaps.lua +++ b/lua/aquila/core/keymaps.lua @@ -9,11 +9,12 @@ set({ 'i', 'n' }, "", 'w') -- i love windows beh set({ 'n', 'v', 'i' }, '', '') -- fix problem when CTRL + Z is exit neovim set('n', '', '') -- fix probelm when CTRL + C in normal mode is exit neovim --- same like alt + up/down at vscode +-- same behavior like alt + up/down in vscode +-- the selected line will move one line up/down set("v", "J", ":m '>+1gv=gv") set("v", "K", ":m '<-2gv=gv") --- when you are in search mode and move with 'n'/'N' for the next matches, the window is stick center +-- pressing `n/N` for the next/prev matches in search mode, will make the matches line center set("n", "n", "nzzzv") set("n", "N", "Nzzzv") @@ -25,6 +26,7 @@ set({ "n", "v" }, "p", "\"+p", { desc = "Paste from system clipboard" }) set("v", "P", '"_dP', { desc = 'Do not lose the " register on paste' }) set("n", "x", '"_x', { desc = 'Do not lose the " register on delete' }) set("n", "c", '"_c', { desc = 'Do not lose the " register on change' }) +set("n", "s", '"_s', { desc = 'Do not lose the " register on replace' }) set("n", "~", 'Alpha', { desc = "Take me home to the place i belong ~~" }) @@ -44,3 +46,24 @@ set("n", "dd", function() end, { expr = true } ) + +-- DUPLICATE VISUAL SELECTION +local duplicate_selection = function() + local save_reg = vim.fn.getreginfo([["]]) + + local visual_mode = vim.fn.mode() + if visual_mode == "v" or visual_mode == "V" then + vim.fn.execute [[noautocmd normal! y`>p]] + else + vim.fn.execute [[noautocmd normal! yP]] + end + + vim.fn.setreg([["]], save_reg.regcontents, save_reg.regtype) +end +set('v', '', duplicate_selection, { desc = 'Duplicate selection' }) +set('v', '', function() + duplicate_selection() + vim.fn.execute [[noautocmd normal! gv]] + end, + { desc = 'Duplicate selection (keep selection)' } +)