1
Fork 0
satellite/home/features/neovim/config/lua/my/keymaps.lua

147 lines
4.2 KiB
Lua
Raw Normal View History

2023-12-21 16:21:14 +01:00
-- TODO: operator for wrapping motion with fold
2023-01-10 02:38:06 +01:00
local M = {}
-- {{{ Helpers
---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
function M.move(from, to, opts)
vim.keymap.set("n", to, from, opts)
vim.keymap.set("n", from, "<Nop>")
end
---Create a textobject defined by some delimiters
---@param from string
---@param to string
---@param name string
2023-12-21 16:21:14 +01:00
---@param opts table|nil
function M.delimitedTextobject(from, to, name, opts)
opts = opts or {}
if opts.desc == nil then
opts.desc = name
end
2023-01-10 02:38:06 +01:00
vim.keymap.set({ "v", "o" }, "i" .. from, "i" .. to, opts)
vim.keymap.set({ "v", "o" }, "a" .. from, "a" .. to, opts)
end
---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
2023-04-17 15:19:41 +02:00
---@param isLocal boolean|nil
function M.nmap(from, to, desc, silent, isLocal)
2023-01-10 02:38:06 +01:00
if silent == nil then
silent = true
end
2023-04-17 15:19:41 +02:00
if isLocal == nil then
isLocal = false
end
vim.keymap.set(
"n",
from,
to,
{ desc = desc, silent = silent, buffer = isLocal }
)
2023-01-10 02:38:06 +01:00
end
-- }}}
function M.setup()
-- {{{ Free up q and Q
M.move("q", "yq", { desc = "Record macro" })
M.move("Q", "yQ")
-- }}}
-- {{{ Easier access to <C-^>
M.move("<C-^>", "<Leader>a", { desc = "[A]lternate file" })
-- }}}
-- {{{ Quit all buffers
2023-01-10 02:38:06 +01:00
M.nmap("Q", ":wqa<cr>", "Save all files and [q]uit")
-- }}}
-- {{{ Replace word in file
M.nmap("<leader>rw", ":%s/<C-r><C-w>/", "[R]eplace [w]ord in file")
-- }}}
-- {{{ Toggle settings
M.nmap(
"<leader>sw",
require("my.helpers.wrapMovement").toggle,
"toggle word [w]rap"
)
-- }}}
2023-01-10 02:38:06 +01:00
-- {{{ Text objects
M.delimitedTextobject("q", '"', "[q]uotes")
M.delimitedTextobject("a", "'", "[a]postrophes")
M.delimitedTextobject("r", "[", "squa[r]e brackets")
-- }}}
-- {{{Diagnostic keymaps
M.nmap("[d", vim.diagnostic.goto_prev, "Goto previous [d]iagnostic")
M.nmap("]d", vim.diagnostic.goto_next, "Goto next [d]iagnostic")
M.move("J", "qj")
2023-01-10 02:38:06 +01:00
M.nmap("J", vim.diagnostic.open_float, "Open current diagnostic")
M.nmap("<leader>D", vim.diagnostic.setloclist, "[D]iagnostic loclist")
-- }}}
-- {{{ Chords (save, clipboard)
-- Different chords get mapped to f keys by a custom script of mine.
-- In the future, I might get this on my keyboard firmware.
vim.keymap.set({ "i", "v" }, "<f10>", "<Esc>", { desc = "Exit insert mode" }) -- Exit inset mode using *jk*
2023-01-10 02:38:06 +01:00
vim.keymap.set({ "n", "v" }, "<f11>", '"+', { desc = "Use global clipboard" }) -- Use global clipboard with *cp*
2023-12-10 12:55:54 +01:00
M.nmap("<f12>", "<cmd>silent write<cr>", "Save current file", true) -- Save using *ji*
2023-01-10 02:38:06 +01:00
-- }}}
-- {{{ Shift-Enter for not continuing the current comment
-- This does not preserve intendation. Not sure what a better solution would look like.
vim.keymap.set("i", "<S-CR>", function()
vim.paste({ "", "" }, -1)
end, { desc = "Insert newline without continuing the current comment" })
-- }}}
2023-03-30 02:54:57 +02:00
-- {{{ Allow quiting basic buffers with "qq"
2023-01-10 02:38:06 +01:00
vim.api.nvim_create_autocmd("FileType", {
pattern = { "help" },
2023-02-24 19:41:43 +01:00
group = vim.api.nvim_create_augroup("BasicBufferQuitting", {}),
2023-01-10 02:38:06 +01:00
callback = function(event)
vim.keymap.set(
"n",
2023-03-30 02:54:57 +02:00
"qq",
2023-01-10 02:38:06 +01:00
"<cmd>close<cr>",
{ buffer = event.buf, silent = true, desc = "[q]uit current buffer" }
)
end,
})
-- }}}
2023-12-21 16:21:14 +01:00
-- {{{ Winblend
vim.api.nvim_create_autocmd("FileType", {
2023-08-28 17:18:26 +02:00
pattern = { "*" },
group = vim.api.nvim_create_augroup("WinblendSettings", {}),
callback = function()
vim.opt.winblend = 0
end,
})
2023-12-21 16:21:14 +01:00
-- }}}
-- {{{ Manage cmdheight
vim.api.nvim_create_autocmd("CmdlineEnter", {
group = vim.api.nvim_create_augroup("SetCmdheightCmdlineEnter", {}),
callback = function()
if not vim.g.inside_venn then
vim.opt.cmdheight = 1
end
end,
})
vim.api.nvim_create_autocmd("CmdlineLeave", {
group = vim.api.nvim_create_augroup("SetCmdheightCmdlineLeave", {}),
callback = function()
if not vim.g.inside_venn then
vim.opt.cmdheight = 0
end
end,
})
-- }}}
2023-01-10 02:38:06 +01:00
end
return M