nvim: add psoxizsh.startup.keybinds

Add our standard keybinds, largely picked from LazyVim's
config/keymap.lua file, filtered down to our taste.

Probably some of these should become configurable in the
future.

References: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
This commit is contained in:
Paul Stemmet 2024-08-25 18:22:53 +00:00
parent 1637296d69
commit 585e13d3d9
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
2 changed files with 82 additions and 0 deletions

View File

@ -74,6 +74,8 @@ function M.early()
{ 'FileType', 'go', function() cmd 'setlocal ts=4 sts=4 sw=4 noexpandtab' end },
{ 'FileType', 'quickfix,netrw', 'setlocal nobuflisted' },
}
require('psoxizsh.startup.keybinds').register_keybinds()
end
---Called after plugins have finished initializing, but before any late user callbacks

View File

@ -0,0 +1,80 @@
---@class psoxizsh.startup.keybinds
local M = {}
---All non plugin specific, overridable keybinds registered by psoxizsh
function M.register_keybinds()
local map = LazyVim.safe_keymap_set
-- Better up/down
map({ 'n', 'x' }, 'j', "v:count == 0 ? 'gj' : 'j'", { desc = 'Down', expr = true, silent = true })
map({ 'n', 'x' }, '<Down>', "v:count == 0 ? 'gj' : 'j'", { desc = 'Down', expr = true, silent = true })
map({ 'n', 'x' }, 'k', "v:count == 0 ? 'gk' : 'k'", { desc = 'Up', expr = true, silent = true })
map({ 'n', 'x' }, '<Up>', "v:count == 0 ? 'gk' : 'k'", { desc = 'Up', expr = true, silent = true })
-- Move Lines
map('n', '<A-j>', '<cmd>m .+1<cr>==', { desc = 'Move Line Down' })
map('n', '<A-k>', '<cmd>m .-2<cr>==', { desc = 'Move Line Up' })
map('i', '<A-j>', '<esc><cmd>m .+1<cr>==gi', { desc = 'Move Line Down' })
map('i', '<A-k>', '<esc><cmd>m .-2<cr>==gi', { desc = 'Move Line Up' })
map('v', '<A-j>', ":m '>+1<cr>gv=gv", { desc = 'Move Line Down' })
map('v', '<A-k>', ":m '<-2<cr>gv=gv", { desc = 'Move Line Up' })
-- Clear search with <esc>
map({ 'i', 'n' }, '<esc>', '<cmd>noh<cr><esc>', { desc = 'Escape and Clear hlsearch' })
-- Ensure the 'n' always searches forward and 'N' always searches backwards,
-- regardless of search mode ('/' '?')
--
-- https://github.com/mhinz/vim-galore#saner-behavior-of-n-and-n
map('n', 'n', "'Nn'[v:searchforward].'zv'", { expr = true, desc = 'Next Search Result' })
map('x', 'n', "'Nn'[v:searchforward]", { expr = true, desc = 'Next Search Result' })
map('o', 'n', "'Nn'[v:searchforward]", { expr = true, desc = 'Next Search Result' })
map('n', 'N', "'nN'[v:searchforward].'zv'", { expr = true, desc = 'Prev Search Result' })
map('x', 'N', "'nN'[v:searchforward]", { expr = true, desc = 'Prev Search Result' })
map('o', 'N', "'nN'[v:searchforward]", { expr = true, desc = 'Prev Search Result' })
-- Add undo break-points
map('i', ',', ',<c-g>u')
map('i', '.', '.<c-g>u')
map('i', ';', ';<c-g>u')
-- Better indenting
map('v', '<', '<gv')
map('v', '>', '>gv')
-- Diagnostics
map('n', ']o', vim.diagnostic.open_float, { desc = 'Line Diagnostics' })
map('n', ']g', M.diagnostic_goto(true), { desc = 'Next Diagnostic' })
map('n', '[g', M.diagnostic_goto(false), { desc = 'Prev Diagnostic' })
map('n', ']e', M.diagnostic_goto(true, 'ERROR'), { desc = 'Next Error' })
map('n', '[e', M.diagnostic_goto(false, 'ERROR'), { desc = 'Prev Error' })
-- Quit
map('n', '<leader>qa', '<cmd>qa<CR>', { desc = 'Quit All' })
-- Highlights under cursor
map('n', '<leader>hi', vim.show_pos, { desc = 'Inspect Pos' })
map('n', '<leader>hI', '<cmd>InspectTree<cr>', { desc = 'Inspect Tree' })
-- Windows
map('n', '<leader>w', '<C-w>', { desc = 'Windows', remap = true })
map('n', '<leader>wd', '<C-W>c', { desc = 'Delete Window', remap = true })
map('n', '<leader>-', '<C-W>s', { desc = 'New Window Below', remap = true })
map('n', '<leader>|', '<C-W>v', { desc = 'New Window Right', remap = true })
LazyVim.toggle.map('<leader>wz', LazyVim.toggle.maximize)
end
---Helper function for moving between diagnostics
---@param next boolean
---@param severity? 'ERROR'|'WARN'|'INFO'|'HINT'
---@return function
function M.diagnostic_goto(next, severity)
local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev
---@cast severity +integer
severity = severity and vim.diagnostic.severity[severity] or nil
return function() go({ severity = severity }) end
end
return M