1
0
Fork 0
dotfiles/.config/nvim/lua/my/keymaps.lua

116 lines
4.3 KiB
Lua
Raw Normal View History

---@format disable
local map = require('my.functions').map
2022-08-08 01:41:01 +02:00
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-16 04:55:49 +02:00
map('n', '<Leader>b', ':buffers<CR>:buffer<Space>', 'Show open buffers')
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')
2024-03-20 19:26:49 +01:00
map('n', '', 'db')
2022-08-13 21:03:21 +02:00
vim.cmd([[map! <C-BS> <C-W>]]) -- map('c', … doesn't work
2024-03-20 19:26:49 +01:00
vim.cmd([[map!  <C-W>]])
2022-08-13 21:03:21 +02:00
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 (clipboard = <C-v>, selection = middle mouse button)
map({ 'v', 'n' }, '<Leader>y', '"+y', 'Yank to system clipboard')
map({ 'v', 'n' }, '<Leader><C-y>', '"*y', 'Yank to system selection')
map('n', '<Leader>p', '"+p', 'Paste from system clipboard')
2023-01-17 17:03:42 +01:00
map('n', '<Leader>P', '"+P', 'Paste from system clipboard')
map('n', '<Leader><C-p>', '"*p', 'Paste from system selection')
map('n', '<Leader><C-M-p>', '"*P', 'Paste from system selection')
-- 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
2022-08-31 02:33:28 +02:00
map('n', '<M-.>', '<C-]>', 'Follow symbol')
map('n', '<M-,>', '<C-T>', 'Go back to previous pos')
map('v', '<Leader>f', 'gq', 'Reformat text')
map('n', '<C-D>', ':bdelete<CR>', 'Close buffer')
2022-09-26 19:04:32 +02:00
-- use ui.select for spelling suggestions so dressing can make it look nice
2022-12-31 23:41:43 +01:00
local function spellsugg_select()
2022-09-26 19:04:32 +02:00
local word = vim.fn.expand('<cword>')
local suggestions = vim.fn.spellsuggest(word)
vim.ui.select(suggestions, {}, vim.schedule_wrap(function(selected)
if selected then
vim.api.nvim_feedkeys('ciw' .. selected, 'n', true)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes('<Esc>', true, true, true),
'n', true)
end
end))
2022-12-31 23:41:43 +01:00
end
map('n', 'z=', spellsugg_select)
-- use easier keys for spell
map('n', '<F17>', '[s', 'Previous misspelled word') -- <S-F5>
map('n', '<F18>', ']s', 'Next misspelled word') -- <S-F6>
map('n', '<F19>', spellsugg_select, 'Spelling suggestions') -- <S-F7>