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

116 lines
4.3 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---@format disable
local map = require('my.functions').map
vim.g.mapleader = ' ' -- <Leader>
vim.g.maplocalleader = ' ' -- <LocalLeader> (2 spaces)
local format = string.format
-- buffers
map('n', '<Leader>b', ':buffers<CR>:buffer<Space>', 'Show open buffers')
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
---- move buffer without moving cursor
map({ 'n', 'v' }, '<M-Up>', '<C-y>')
map({ 'n', 'v' }, '<M-Down>', '<C-e>')
-- windows
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
-- remove word
map('n', '<C-BS>', 'db')
map('n', '', 'db')
vim.cmd([[map! <C-BS> <C-W>]]) -- map('c', … doesn't work
vim.cmd([[map!  <C-W>]])
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')
-- 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
)
-- 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')
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))
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', 'v' }, '<C-F>', '==') -- re-indent line
-- select text with shift + arrow
for _, key in ipairs({ 'Left', 'Up', 'Down', 'Right' }) do
map({ 'n', 'i' }, format('<S-%s>', key), format('<Esc>v<%s>', key))
map({ 'v' }, format('<S-%s>', key), format('<%s>', key))
end
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')
-- use ui.select for spelling suggestions so dressing can make it look nice
local function spellsugg_select()
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))
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>