Files
nixos/home-modules/explicit-configs/nvim/lua/keymappings.lua
T

79 lines
3.6 KiB
Lua
Raw Normal View History

local map = vim.keymap.set
2025-05-03 23:42:03 -07:00
map('n', '<Space>', '<Nop>', { silent = true })
map('i', ',m', '<Esc>', { silent = true })
2026-11-20 12:31:01 -08:00
-- Preserve a pending comma from the ,m mapping when leaving insert mode.
map('i', '<C-c>', '<Esc>', { silent = true })
2025-05-03 23:42:03 -07:00
for _, mode in ipairs({ 'n', 'i', 'v', 's', 'x', 'c', 'o', 't' }) do
map(mode, '<C-g>', '<Esc>', { silent = true })
end
map('i', '<C-l>', '<Right>', { silent = true })
2025-05-03 23:42:03 -07:00
for _, direction in ipairs({ 'h', 'j', 'k', 'l' }) do
map('n', '<leader>w' .. direction, '<C-w>' .. direction, { desc = 'Move to window ' .. direction })
end
map('n', '<leader>fw', '<cmd>write<cr>', { desc = 'Write file' })
map('n', '<leader><leader>', '<C-^>', { desc = 'Alternate buffer' })
2025-05-03 23:42:03 -07:00
2026-11-20 14:27:41 -08:00
map('n', '<leader>ff', function()
-- Include dotfiles; rg (telescope's preferred backend) hides them by default,
-- while a plain --hidden would flood results with .git internals.
if vim.fn.executable('rg') == 1 then
require('telescope.builtin').find_files({
find_command = { 'rg', '--files', '--hidden', '--glob', '!.git', '--color', 'never' },
})
else
require('telescope.builtin').find_files({ hidden = true })
end
end, { desc = 'Find files' })
map('n', '<leader>fb', '<cmd>Oil .<cr>', { desc = 'Browse current directory' })
2026-11-20 14:27:41 -08:00
map('n', '<leader>fc', function()
-- With no file in the buffer there is no directory to search; warn rather
-- than let expand('%:p:h') fall back to the cwd (the whole project).
local dir
if vim.bo.filetype == 'oil' then
dir = require('oil').get_current_dir()
else
local bufname = vim.api.nvim_buf_get_name(0)
if bufname ~= '' then
dir = vim.fn.fnamemodify(bufname, ':p:h')
end
end
if not dir or vim.fn.isdirectory(dir) ~= 1 then
vim.notify('No file to find alongside of (try <leader>ff)', vim.log.levels.WARN)
return
end
require('telescope').extensions.file_browser.file_browser({ path = dir })
end, { desc = 'Fuzzy-browse file directory' })
map('n', '<leader>fo', '<cmd>Oil<cr>', { desc = 'Browse file directory' })
map('n', '<leader>ps', function()
require('telescope.builtin').live_grep({
-- See <leader>ff: include dotfiles, but not .git's contents.
additional_args = { '--hidden', '--glob', '!.git' },
})
end, { desc = 'Search text' })
map('n', '<leader>bs', '<cmd>Telescope buffers<cr>', { desc = 'Search buffers' })
map('n', '<leader>sr', '<cmd>GrugFar<cr>', { desc = 'Search and replace' })
map('n', '<leader>rsr', '<cmd>GrugFarWithin<cr>', { desc = 'Open range replacement' })
2025-05-03 23:42:03 -07:00
map({ 'n', 'x' }, '<leader>ca', vim.lsp.buf.code_action, { desc = 'Code action' })
map('n', '<leader>lgd', vim.lsp.buf.definition, { desc = 'Go to definition' })
map('n', '<leader>lgD', vim.lsp.buf.declaration, { desc = 'Go to declaration' })
map('n', '<leader>lgr', vim.lsp.buf.references, { desc = 'Find references' })
map('n', '<leader>lgi', vim.lsp.buf.implementation, { desc = 'Go to implementation' })
map('n', '<leader>lr', vim.lsp.buf.rename, { desc = 'Rename symbol' })
map('n', '<leader>lf', function()
require('conform').format({ async = true, lsp_format = 'fallback' })
end, { desc = 'Format buffer' })
map('x', '<leader>lrf', vim.lsp.buf.format, { desc = 'Format selection' })
map('n', 'K', function()
vim.lsp.buf.hover({ border = 'rounded', max_width = 80 })
end, { desc = 'Hover documentation' })
2025-05-03 23:42:03 -07:00
map('n', '<leader>lld', function()
vim.diagnostic.open_float({ scope = 'line', border = 'rounded' })
end, { desc = 'Line diagnostics' })
map('n', '<leader>lwd', '<cmd>Trouble diagnostics toggle<cr>', { desc = 'Workspace diagnostics' })
map('n', '<leader>ldd', '<cmd>Trouble diagnostics toggle filter.buf=0<cr>', { desc = 'Buffer diagnostics' })