Skip to content

Commit

Permalink
chore(Keymaps): refactor and add duplicate selection keymap
Browse files Browse the repository at this point in the history
  • Loading branch information
rizkyilhampra committed Jan 28, 2024
1 parent 7e74151 commit d449e0a
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lua/aquila/core/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ set({ 'i', 'n' }, "<C-s>", '<cmd>w<CR>') -- i love windows beh
set({ 'n', 'v', 'i' }, '<C-z>', '<Nop>') -- fix problem when CTRL + Z is exit neovim
set('n', '<C-c>', '<Nop>') -- 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 '>+1<CR>gv=gv")
set("v", "K", ":m '<-2<CR>gv=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")

Expand All @@ -25,6 +26,7 @@ set({ "n", "v" }, "<Leader>p", "\"+p", { desc = "Paste from system clipboard" })
set("v", "<leader>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", "<Leader>~", '<cmd>Alpha<CR>', { desc = "Take me home to the place i belong ~~" })

Expand All @@ -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', '<C-d>', duplicate_selection, { desc = 'Duplicate selection' })
set('v', '<C-M-d>', function()
duplicate_selection()
vim.fn.execute [[noautocmd normal! gv]]
end,
{ desc = 'Duplicate selection (keep selection)' }
)

0 comments on commit d449e0a

Please sign in to comment.