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

88 lines
2.5 KiB
Lua
Raw Normal View History

2022-03-09 19:03:04 +01:00
local helpers = require("my.helpers")
local arpeggio = require("my.plugins.arpeggio")
2022-03-09 19:03:04 +01:00
2022-01-30 19:10:57 +01:00
local M = {}
2022-12-26 20:07:10 +01:00
-- {{{ Helpers
2022-09-12 13:50:02 +02:00
-- Performs a basic move operation
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-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-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
-- }}}
-- {{{ Easier access to C^
2022-10-09 03:28:51 +02:00
M.move("<C-^>", "<Leader>a", { desc = "Go to previous 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-09 02:32:25 +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
vim.cmd "q"
end, { desc = "Quit current buffer" })
2022-10-09 03:28:51 +02:00
vim.keymap.set("n", "Q", ":wqa<cr>", { desc = "Save all files and quit" })
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{ Replace word in file
2022-12-09 02:32:25 +01:00
vim.keymap.set("n", "<leader>rw", ":%s/<C-r><C-w>/", { desc = "Replace word in file" })
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{ Text objects
2022-10-13 01:05:38 +02:00
M.delimitedTextobject("q", '"', "quotes")
M.delimitedTextobject("a", "'", "'")
M.delimitedTextobject("r", "[", "square brackets")
2022-12-26 20:07:10 +01:00
-- }}}
-- {{{Diagnostic keymaps
do
local opts = function(desc)
return { noremap = true, silent = true, desc = desc }
end
2022-10-13 01:05:38 +02:00
2022-12-26 20:07:10 +01:00
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts("Goto previous diagnostic"))
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts("Goto next diagnostic"))
vim.keymap.set('n', 'J', vim.diagnostic.open_float, opts("Open current diagnostic"))
vim.keymap.set('n', '<leader>J', vim.diagnostic.setloclist, opts("Set diagnostics loclist"))
end
-- }}}
-- {{{ Chords
2022-07-19 20:19:36 +02:00
if arpeggio ~= nil then
2022-10-09 03:28:51 +02:00
arpeggio.chordSilent("n", "ji", ":silent :write<cr>") -- Saving
2022-07-19 20:19:36 +02:00
arpeggio.chord("i", "jk", "<Esc>") -- Remap Esc to jk
arpeggio.chord("nv", "cp", "\"+") -- Press cp to use the global clipboard
2022-10-09 03:28:51 +02:00
end
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-09 02:32:25 +01:00
f = { name = "Files" },
g = { name = "Go to" },
r = { name = "Rename / Replace / Reload" },
l = { name = "Local" },
2022-10-09 03:28:51 +02:00
v = "which_key_ignore"
}
})
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-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