.files/.config/nvim/lua/maps.lua

60 lines
2.4 KiB
Lua

vim.g.mapleader = " "
vim.g.maplocalleader = " "
---@param bind string
---@param cmd function | string
---@param desc string
---@param userargs? table
local vnoremap = function(bind, cmd, desc, userargs)
local args = userargs or {}
args.desc = desc
vim.keymap.set("v", bind, cmd, args)
end
---@param bind string
---@param cmd function | string
---@param desc string
---@param userargs? table
local nnoremap = function(bind, cmd, desc, userargs)
local args = userargs or {}
args.desc = desc
vim.keymap.set("n", bind, cmd, args)
end
-- Move
vnoremap("J", ":m '>+1<CR>gv", "Move line down")
vnoremap("K", ":m '<-2<CR>gv", "Move line up")
vnoremap(">", ">gv", "Shift right (keep selection)")
vnoremap("<", "<gv", "Shift left (keep selection)")
nnoremap("<C-d>", "<C-d>zz", "Page down (then center)")
nnoremap("<C-u>", "<C-u>zz", "Page up (then center)")
nnoremap("gd", "gdzz", "Go to definition (then center)")
nnoremap("gD", "<C-w>vgd", "Go to definition in a new window", { remap = true })
nnoremap("``", "``zz", "Jump to `` (then center)")
nnoremap("J", "mzJ`z", "Join line (and restore cursor)")
-- Better clipboard
vim.keymap.set({ "n", "x", "v" }, "<leader>d", '"_d', { desc = "Delete (void register)" })
vim.keymap.set({ "n", "x", "v" }, "<leader>c", '"_dc', { desc = "Change (void regsiter)" })
vim.keymap.set({ "n", "x", "v" }, "<leader>p", '"_dP', { desc = "Paste (void register)" })
vim.keymap.set({ "n", "x", "v" }, "<leader>y", '"+y', { desc = "Yank (system clipboard)" })
-- Replace selected token
nnoremap("<leader>pv", function() vim.cmd("Oil") end, "Open oil")
nnoremap("<leader>+x", ":!chmod +x %", "Make executable")
nnoremap("<leader>-x", ":!chmod -x %", "Make non-executable")
vim.keymap.set("c", "#capl", [[\(.\{-}\)]], { desc = "Capture more" })
vim.keymap.set("c", "#capm", [[\(.*\)]], { desc = "Capture less" })
-- Diagnostics
nnoremap("<leader>e", vim.diagnostic.open_float, "Open diagnostic as float")
nnoremap("<leader>pe", function() vim.diagnostic.jump { count = -1, float = true } end, "Previous diagnostic")
nnoremap("<leader>ne", function() vim.diagnostic.jump { count = 1, float = true } end, "Next diagnostic")
-- Resize window using shift + arrow keys
-- Credit: github.com/GrizzlT
vim.keymap.set("n", "<S-Up>", "<cmd>resize +2<cr>")
vim.keymap.set("n", "<S-Down>", "<cmd>resize -2<cr>")
vim.keymap.set("n", "<S-Left>", "<cmd>vertical resize -2<cr>")
vim.keymap.set("n", "<S-Right>", "<cmd>vertical resize +2<cr>")