1
Fork 0
satellite/dotfiles/neovim/lua/my/keymaps.lua

116 lines
3 KiB
Lua
Raw Normal View History

2022-03-09 19:03:04 +01:00
local helpers = require("my.helpers")
2022-01-30 19:10:57 +01:00
local M = {}
2022-12-26 20:07:10 +01:00
-- {{{ Helpers
2022-12-27 14:02:03 +01:00
---Performs a basic move operation
---Moves a keybind to a different set of keys.
---Useful for moving built in keybinds out of the way.
---@param from string
---@param to string
---@param opts table|nil
2022-10-09 03:28:51 +02:00
function M.move(from, to, opts)
vim.keymap.set("n", to, from, opts)
2022-09-12 13:50:02 +02:00
vim.keymap.set("n", from, "<Nop>")
end
2022-12-27 14:02:03 +01:00
---Create a textobject defined by some delimiters
---@param from string
---@param to string
---@param name string
---@param perhapsOpts table|nil
2022-10-13 01:05:38 +02:00
function M.delimitedTextobject(from, to, name, perhapsOpts)
local opts = helpers.mergeTables(perhapsOpts or {}, { desc = name })
vim.keymap.set({ "v", "o" }, "i" .. from, "i" .. to, opts)
vim.keymap.set({ "v", "o" }, "a" .. from, "a" .. to, opts)
end
2022-12-27 14:02:03 +01:00
---Helper to create a normal mode mapping and give it some description.
---@param from string
---@param to string|function
---@param desc string
---@param silent boolean|nil
function M.nmap(from, to, desc, silent)
if silent == nil then
silent = true
end
vim.keymap.set("n", from, to, { desc = desc })
end
2022-12-26 20:07:10 +01:00
-- }}}
2022-10-13 01:05:38 +02:00
2022-01-30 19:10:57 +01:00
function M.setup()
2022-12-26 20:07:10 +01:00
-- {{{ Free up q and Q
2022-10-09 03:28:51 +02:00
M.move("q", "yq", { desc = "Record macro" })
2022-09-12 13:50:02 +02:00
M.move("Q", "yQ")
2022-12-26 20:07:10 +01:00
-- }}}
2022-12-27 14:02:03 +01:00
-- {{{ Easier access to <C-^>
M.move("<C-^>", "<Leader>a", { desc = "[A]lternate file" })
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{ Quit current buffer / all buffers
2022-12-09 02:32:25 +01:00
vim.keymap.set({ "n", "v" }, "<leader>q", function()
2022-10-14 13:44:47 +02:00
local buf = vim.api.nvim_win_get_buf(0)
-- Only save if file is writable
2022-12-27 14:02:03 +01:00
if vim.bo[buf].modifiable and not vim.bo[buf].readonly then
vim.cmd([[write]])
end
2022-10-14 13:44:47 +02:00
2022-12-27 14:02:03 +01:00
vim.cmd("q")
end, { desc = "[q]uit current buffer" })
2022-10-14 13:44:47 +02:00
2022-12-27 14:02:03 +01:00
M.nmap("Q", ":wqa<cr>", "Save all files and [q]uit")
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{ Replace word in file
2022-12-27 14:02:03 +01:00
M.nmap("<leader>rw", ":%s/<C-r><C-w>/", "[R]eplace [w]ord in file")
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{ Text objects
2022-12-27 14:02:03 +01:00
M.delimitedTextobject("q", '"', "[q]uotes")
M.delimitedTextobject("a", "'", "[a]postrophes")
M.delimitedTextobject("r", "[", "squa[r]e brackets")
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{Diagnostic keymaps
2022-12-27 14:02:03 +01:00
M.nmap("[d", vim.diagnostic.goto_prev, "Goto previous [d]iagnostic")
M.nmap("]d", vim.diagnostic.goto_next, "Goto next [d]iagnostic")
M.nmap("J", vim.diagnostic.open_float, "Open current diagnostic")
M.nmap("<leader>D", vim.diagnostic.setloclist, "[S]iagnostic loclist")
2022-12-26 20:07:10 +01:00
-- }}}
2022-12-27 14:02:03 +01:00
-- {{{ Chords (exit insert mode, save, clipboard)
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{ Set up which-key structure
2022-10-09 03:28:51 +02:00
local status, wk = pcall(require, "which-key")
if status then
wk.register({
["<leader>"] = {
2022-12-27 14:02:03 +01:00
f = { name = "[F]iles" },
g = { name = "[G]o to" },
r = { name = "[R]ename / [R]eplace / [R]eload" },
l = { name = "[L]ocal" },
w = { name = "[W]orkspace" },
v = "which_key_ignore",
},
2022-10-09 03:28:51 +02:00
})
2022-07-19 20:19:36 +02:00
end
2022-12-26 20:07:10 +01:00
-- }}}
2022-05-10 14:28:36 +02:00
2022-12-27 14:02:03 +01:00
vim.api.nvim_create_autocmd({ "FileType" }, {
pattern = {
"help",
"man",
"notify",
"lspinfo"
},
callback = function(event)
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true, desc = "[q]uit current buffer" })
end,
})
2022-07-19 20:19:36 +02:00
return M
2022-01-30 20:19:35 +01:00
end
2022-02-01 00:06:48 +01:00
return M