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

87 lines
3.0 KiB
Lua
Raw Normal View History

2022-08-08 01:41:01 +02:00
function map(mode, shortcut, command)
2022-08-08 11:07:10 +02:00
vim.keymap.set(mode, shortcut, command, { noremap = true, silent = true })
2022-08-08 01:41:01 +02:00
end
vim.g.mapleader = ' ' -- <Leader>
vim.g.maplocalleader = ' ' -- <LocalLeader> (2 spaces)
2022-08-08 19:12:09 +02:00
2022-08-11 22:06:14 +02:00
local format = string.format
-- buffers
2022-08-13 01:34:38 +02:00
map('n', '<Leader>b', ':buffers<CR>:buffer<Space>')
2022-08-11 22:06:14 +02:00
for key, cmd in pairs({ Left = 'bprevious', Right = 'bnext' }) do
map('n', format('<M-%s>', key), format(':%s<cr>', cmd))
map('i', format('<M-%s>', key), format('<esc>:%s<cr>', cmd))
end
2022-08-13 01:34:38 +02:00
---- move buffer without moving cursor
map({ 'n', 'v' }, '<M-Up>', '<C-y>')
map({ 'n', 'v' }, '<M-Down>', '<C-e>')
2022-08-13 01:34:38 +02:00
-- windows
2022-08-11 22:06:14 +02:00
for key, letter in pairs({ Left = 'h', Down = 'j', Up = 'k', Right = 'l' }) do
map('n', format('<M-S-%s>', key), format(':wincmd %s<cr>', letter))
map('i', format('<M-S-%s>', key), format('<esc>:wincmd %s<cr>', letter))
end
2022-08-08 18:10:10 +02:00
-- remove word
2022-08-13 21:03:21 +02:00
map('n', '<C-BS>', 'db')
vim.cmd([[map! <C-BS> <C-W>]]) -- map('c', … doesn't work
map('n', '<M-BS>', 'db') -- terminal sends M-BS for C-BS
vim.cmd([[map! <M-BS> <C-W>]])
map('n', '<C-Del>', 'dw')
map('i', '<C-Del>', '<C-O>dw')
2022-08-09 11:24:22 +02:00
-- 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
2022-08-12 14:05:53 +02:00
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 = ''
2022-08-12 14:05:53 +02:00
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
)
-- 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))
2022-08-12 14:05:53 +02:00
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
)
2022-08-12 19:20:30 +02:00
map({ 'n', 'v' }, '<C-F>', '==') -- re-indent line
-- select text with shift + arrow
for _, key in ipairs({ 'Left', 'Up', 'Down', 'Right' }) do
2022-08-13 01:34:38 +02:00
map({ 'n', 'i' }, format('<S-%s>', key), format('<Esc>v<%s>', key))
map({ 'v' }, format('<S-%s>', key), format('<%s>', key))
end
2022-08-11 22:22:01 +02:00
map('n', '<F9>', ':Lexplore 20<cr>') -- file explorer, 20% wide
map('i', '<F9>', '<Esc>:Lexplore 20<cr>')
map('n', '<Leader><cr>', '<C-]>') -- follow links in help