2024-11-28 13:13:57 -08:00
|
|
|
local g = vim.g
|
2024-11-28 15:33:05 -08:00
|
|
|
local set = vim.keymap.set
|
|
|
|
local f = string.format
|
2024-11-28 13:13:57 -08:00
|
|
|
|
|
|
|
g.mapleader = ' '
|
|
|
|
g.maplocalleader = '\\'
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
-- Disable arrow key movement -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for _, key in ipairs({ '<DOWN>', '<UP>', '<LEFT>', '<RIGHT>' }) do
|
|
|
|
set({ 'n', 'i' }, key, '<NOP>')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Better up/down -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for dir, key in pairs({ Down = 'j', Up = 'k' }) do
|
|
|
|
set(
|
|
|
|
{ 'n', 'x' },
|
|
|
|
key,
|
2024-11-30 19:08:20 -08:00
|
|
|
f("v:count == 0 ? 'g%s' : '%s'", key, key),
|
2024-11-28 15:33:05 -08:00
|
|
|
{ desc = dir, expr = true, silent = true }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Resize windows -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for key, dir in pairs({
|
2024-12-01 17:10:02 -08:00
|
|
|
Up = 'increase',
|
|
|
|
Down = 'decrease',
|
|
|
|
Left = 'decrease',
|
|
|
|
Right = 'increase',
|
2024-11-28 15:33:05 -08:00
|
|
|
}) do
|
2024-12-01 17:10:02 -08:00
|
|
|
local sign = (dir == 'increase') and '+' or '-'
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
f('<C-%s>', key),
|
|
|
|
f('<CMD>resize %s4<CR>', sign),
|
|
|
|
{ desc = f('%s window height', dir) }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Buffers -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for key, dir in pairs({
|
|
|
|
['<S-H>'] = 'previous',
|
|
|
|
['<S-L>'] = 'next',
|
|
|
|
['[b'] = 'previous',
|
|
|
|
[']b'] = 'next',
|
|
|
|
}) do
|
2024-12-01 17:10:02 -08:00
|
|
|
-- previous -> prev
|
|
|
|
local prettyDir = dir:sub(1, 4)
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
key,
|
|
|
|
f('<CMD>b%s<CR>', dir),
|
|
|
|
{ desc = f('%s buffer', prettyDir) }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-12-01 17:10:02 -08:00
|
|
|
set({ 'n' }, '<LEADER>bD', '<CMD>bd<CR>', { desc = 'delete buffer and window' })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
-- Clear search & refresh -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
set(
|
|
|
|
{ 'n', 'i' },
|
|
|
|
'<ESC>',
|
|
|
|
'<CMD>noh<CR><ESC>',
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = 'escape and clear hlsearch' }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
'<LEADER>ur',
|
|
|
|
'<CMD>nohlsearch<BAR>diffupdate<BAR>normal! <C-l><CR>',
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = 'redraw / clear hlsearch / diff update' }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
-- Better n & N -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for _, mode in ipairs({ 'n', 'x', 'o' }) do
|
|
|
|
local zv = (mode == 'n') and ".'zv'" or ''
|
|
|
|
|
|
|
|
set(
|
|
|
|
{ mode },
|
|
|
|
'n',
|
|
|
|
f("'Nn'[v:searchforward]%s", zv),
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = 'next search result', expr = true }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
set(
|
|
|
|
{ mode },
|
|
|
|
'N',
|
|
|
|
f("'nN'[v:searchforward]%s", zv),
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = 'previous search result', expr = true }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Undo break-points -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for _, char in ipairs({ ',', '.', ';' }) do
|
|
|
|
set({ 'i' }, char, f('%s<C-g>u', char))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Search docs (keywordprog) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
-- https://til.codeinthehole.com/posts/about-how-to-use-keywordprg-effectively/
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
'<LEADER>K',
|
|
|
|
'<CMD>norm! K<CR>',
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = 'search <KEYWORDPROG> for word' }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
-- Better indenting -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for _, char in ipairs({ '<', '>' }) do
|
2024-12-01 17:10:02 -08:00
|
|
|
local desc = 'indent ' .. (char == '<' and 'left' or 'right')
|
|
|
|
|
|
|
|
set({ 'v' }, char, f('%sgv', char), { desc = desc })
|
2024-11-28 15:33:05 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Commenting -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-12-01 17:10:02 -08:00
|
|
|
for key, dir in pairs({ o = 'below', O = 'above' }) do
|
2024-11-28 15:33:05 -08:00
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
f('gc%s', key),
|
|
|
|
f('%s<ESC>Vcx<ESC><CMD>normal gcc<CR>fxa<BS>', key),
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = f('add comment %s', dir) }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- New files -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-12-01 17:10:02 -08:00
|
|
|
set({ 'n' }, '<LEADER>fn', '<CMD>enew<CR>', { desc = 'new file' })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
-- Locations/quickfixes -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-12-01 17:10:02 -08:00
|
|
|
set({ 'n' }, '<LEADER>xl', '<CMD>lopen<CR>', { desc = 'location list' })
|
|
|
|
set({ 'n' }, '<LEADER>xq', '<CMD>copen<CR>', { desc = 'quickfix list' })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
2024-12-01 17:10:02 -08:00
|
|
|
for key, dir in pairs({ ['['] = 'previous', [']'] = 'next' }) do
|
|
|
|
-- previous -> prev
|
|
|
|
local cmd = dir:sub(1, 4)
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
f('%sq', key),
|
|
|
|
f('<CMD>c%s<CR>', cmd),
|
|
|
|
{ desc = f('%s quickfix', dir) }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Diagnostics -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
set({ 'n' }, '<LEADER>cd', function()
|
|
|
|
vim.diagnostic.open_float()
|
2024-12-01 17:10:02 -08:00
|
|
|
end, { desc = 'line diagnostics' })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
local function goto_diagnostic(next, severity)
|
|
|
|
local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev
|
|
|
|
severity = severity and vim.diagnostic.severity[severity] or nil
|
|
|
|
return function()
|
|
|
|
go({ severity = severity })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for key, sev in pairs({
|
2024-12-01 17:10:02 -08:00
|
|
|
d = { nil, 'diagnostic' },
|
|
|
|
e = { 'ERROR', 'error' },
|
|
|
|
w = { 'WARN', 'warning' },
|
2024-11-28 15:33:05 -08:00
|
|
|
}) do
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
f(']%s', key),
|
|
|
|
goto_diagnostic(true, sev[1]),
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = f('next %s', sev[2]) }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
f('[%s', key),
|
|
|
|
goto_diagnostic(false, sev[1]),
|
2024-12-01 17:10:02 -08:00
|
|
|
{ desc = f('previous %s', sev[2]) }
|
2024-11-28 15:33:05 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Quit -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-12-01 17:10:02 -08:00
|
|
|
set({ 'n' }, '<LEADER>qq', '<CMD>qa<CR>', { desc = 'quit all' })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
-- Inspect -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-12-01 17:10:02 -08:00
|
|
|
set({ 'n' }, '<LEADER>ui', vim.show_pos, { desc = 'inspect position' })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
-- Window management -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2024-12-01 17:10:02 -08:00
|
|
|
set({ 'n' }, '<LEADER>w', '<C-w>', { desc = 'windows', remap = true })
|
|
|
|
set({ 'n' }, '<LEADER>-', '<C-w>s', { desc = 'split below', remap = true })
|
|
|
|
set({ 'n' }, '<LEADER>|', '<C-w>v', { desc = 'split right', remap = true })
|
|
|
|
set({ 'n' }, '<LEADER>wd', '<C-w>c', { desc = 'delete window', remap = true })
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
-- Tab management -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
|
|
|
for key, act in pairs({
|
2024-12-01 17:10:02 -08:00
|
|
|
l = 'last',
|
|
|
|
o = 'only',
|
|
|
|
f = 'first',
|
|
|
|
['<TAB>'] = 'new',
|
|
|
|
[']'] = 'next',
|
|
|
|
d = 'close',
|
|
|
|
['['] = 'previous',
|
2024-11-28 15:33:05 -08:00
|
|
|
}) do
|
2024-12-01 17:10:02 -08:00
|
|
|
local desc = (act == 'only') and 'close other tabs' or f('%s tab', act)
|
2024-11-28 15:33:05 -08:00
|
|
|
|
|
|
|
set(
|
|
|
|
{ 'n' },
|
|
|
|
f('<LEADER><TAB>%s', key),
|
|
|
|
f('<CMD>tab%s<CR>', act:lower()),
|
|
|
|
{ desc = desc }
|
|
|
|
)
|
|
|
|
end
|