dotfiles/.config/nvim/lua/my/keymaps.lua

77 lines
2.5 KiB
Lua

function map(mode, shortcut, command)
vim.keymap.set(mode, shortcut, command, { noremap = true, silent = true })
end
vim.g.mapleader = ' ' -- <Leader>
-- buffers
map('n', '<Leader>b', ':buffers<CR>:buffer<Space>')
map('n', '<M-Left>', ':bprevious<cr>')
map('n', '<M-Right>', ':bnext<cr>')
map('i', '<M-Left>', '<esc>:bprevious<cr>')
map('i', '<M-Right>', '<esc>:bnext<cr>')
--windows
map('n', '<M-S-Left>', ':wincmd h<cr>')
map('n', '<M-S-Down>', ':wincmd j<cr>')
map('n', '<M-S-Up>', ':wincmd k<cr>')
map('n', '<M-S-Right>', ':wincmd l<cr>')
map('i', '<M-S-Left>', '<esc>:wincmd h<cr>')
map('i', '<M-S-Down>', '<esc>:wincmd j<cr>')
map('i', '<M-S-Up>', '<esc>:wincmd k<cr>')
map('i', '<M-S-Right>', '<esc>:wincmd l<cr>')
-- remove word
map('n', '<M-BS>', 'db')
vim.cmd([[map! <M-BS> <C-W>]]) -- TODO: figure out how to do that with lua
map('n', '<C-Del>', 'dw')
map('i', '<C-Del>', '<C-O>dw')
-- remove whitespace around cursor
map({ 'n', 'i' }, '<C-S-Del>',
function()
local row, pos_cursor = unpack(vim.api.nvim_win_get_cursor(0))
pos_cursor = pos_cursor + 1 -- api / lua is off by one
local line = vim.api.nvim_get_current_line()
local pos_start, pos_end = 0, 0
while (pos_start ~= nil) do
if (pos_start <= pos_cursor and pos_end >= pos_cursor) then
local before = line:sub(1, pos_start - 1)
local after = line:sub(pos_end + 1)
local space = ''
if (before:len() > 0 and after:len() > 0) then
space = ' '
end
vim.api.nvim_set_current_line(before .. space .. after)
vim.api.nvim_win_set_cursor(0, { row, pos_start - 1 })
break
end
pos_start, pos_end = line:find('%s+', pos_end + 1)
end
end
)
-- move buffer without moving cursor
map('n', '<M-Up>', '<C-y>')
map('n', '<M-Down>', '<C-e>')
-- system clipboard
map('v', '<Leader>y', '"+y')
map('n', '<Leader>p', '"+p')
-- toggle between beginning of line and beginning of text
map({ 'n', 'i', 'v' }, '<Home>',
function()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
if (col == 0) then
local col_new = vim.api.nvim_get_current_line():match('^%s*'):len()
vim.api.nvim_win_set_cursor(0, { row, col_new })
else
vim.api.nvim_win_set_cursor(0, { row, 0 })
end
end
)
map('n', '<C-F>', '==') -- re-indent line