feat(config): added custom keymaps

This commit is contained in:
Sven Vogel 2024-10-31 10:54:50 +01:00
parent 596c33595c
commit ecaf12f283
2 changed files with 133 additions and 0 deletions

View File

@ -1,2 +1,3 @@
require("config.lazy")
require("config.opts")
require("config.keys")

132
lua/config/keys.lua Normal file
View File

@ -0,0 +1,132 @@
local keymaps = {
--
-- Write buffer to disk
-- ..............................................
{
mode = { 'n', 'i' },
keys = '<C-s>',
cmd = vim.cmd.write,
desc = "Write buffer to disk"
},
{
mode = { 'n', 'i' },
keys = '<C-z>',
cmd = vim.cmd.undo,
desc = "Undo last edit"
},
{
mode = { 'n', 'i' },
keys = '<C-y>',
cmd = vim.cmd.redo,
desc = "Redo last edit that was undone"
},
--
-- Shift line(s) by one shiftwidth
-- ..............................................
{
mode = { 'n', 'i' },
keys = '<S-tab>',
cmd = '<cmd>:<<cr>',
desc = "Shift cursor line left"
},
{
mode = { 'n', 'i' },
keys = '<tab>',
cmd = '<cmd>:><cr>',
desc = "Shift cursor line right"
},
{
mode = { 'v' },
keys = '<tab>',
cmd = '>gv',
desc = "Shift selection in visual mode right"
},
{
mode = { 'v' },
keys = '<S-tab>',
cmd = '<gv',
desc = "Shift selection in visual right"
},
--
-- Buffer management
-- ..............................................
{
mode = { 'n', 'i' },
keys = '<C-t>',
cmd = '<cmd>:enew<cr>',
desc = "Edit new, unnamed buffer"
},
--
-- Window controls
-- ..............................................
{
mode = { 'n', 'i' },
keys = '<C-w>v',
cmd = '<cmd>:vsplit<cr>',
desc = "Split window vertically"
},
{
mode = { 'n', 'i' },
keys = '<C-w>h',
cmd = '<cmd>:split<cr>',
desc = "Split window horizontally"
},
{
mode = { 'n' },
keys = 'q',
cmd = function()
-- list of file types to ignore when counting split buffers
local ignore = { "neo-tree" }
-- number of windows per tab
local wins = vim.api.nvim_tabpage_list_wins(0)
-- count of open windows without those on ignore list
local count = 0
-- check each window for its type
for _, win in ipairs(wins) do
local buf = vim.api.nvim_win_get_buf(win)
local buf_type = vim.api.nvim_buf_get_option(buf, 'buftype')
local file_type = vim.api.nvim_buf_get_option(buf, 'filetype')
if buf_type == '' then
-- check if buffer is in ignore
in_ignore = false
for _, v in ipairs(ignore) do
if v == file_type then
in_ignore = true
break
end
end
if not in_ignore then
count = count + 1
end
end
end
if count == 1 then
-- only one buffer without any split
-- account for barbar specific behavior
vim.cmd('BufferClose')
else
-- close window
vim.cmd('q')
end
end,
desc = "Close buffer"
}
}
for _, keymap in pairs(keymaps) do
local mode = keymap.mode
local lhs = keymap.keys
local rhs = keymap.cmd
local opts = {
desc = keymap.desc,
noremap = keymap.noremap
}
vim.keymap.set(mode, lhs, rhs, opts)
end