Skip to content

Commit

Permalink
Simplify configuring keymaps
Browse files Browse the repository at this point in the history
* Reuse `vim.keymap.set` as `local map` when possible
* Remove redundant `silent = true` options
* Use recommended mappings for gitsigns.nvim
  • Loading branch information
dietrichm committed Dec 29, 2024
1 parent f7311d5 commit e03a304
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 33 deletions.
2 changes: 1 addition & 1 deletion nvim/.config/nvim/after/ftplugin/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ vim.opt_local.spell = true
vim.cmd.compiler('markdown')

-- Wrap visual selection in link.
vim.keymap.set('v', '<Leader>cl', '<Plug>VSurround]%a(', { buffer = true, remap = true, silent = true })
vim.keymap.set('v', '<Leader>cl', '<Plug>VSurround]%a(', { buffer = true, remap = true })

-- Surround text with `*` places double asterisks.
vim.b.surround_42 = '**\r**'
2 changes: 1 addition & 1 deletion nvim/.config/nvim/after/ftplugin/php.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ vim.cmd.iabbrev('<buffer>', '<expr>', 'ns', [[substitute(tr(expand('%:p:h'), '/'
vim.cmd.iabbrev('<buffer>', '<expr>', 'cls', [[expand('%:t:r')]])
vim.cmd.iabbrev('<buffer>', 'strict', [[declare(strict_types=1);]])

vim.keymap.set('n', '<Leader>af', [[:PhpFields<CR>]], { buffer = true, silent = true })
vim.keymap.set('n', '<Leader>af', [[:PhpFields<CR>]], { buffer = true })
2 changes: 1 addition & 1 deletion nvim/.config/nvim/lua/dotfiles/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ diagnostic.config({
},
})

vim.keymap.set('n', '<Leader>q', diagnostic.setloclist, { silent = true })
vim.keymap.set('n', '<Leader>q', diagnostic.setloclist)

vim.api.nvim_create_autocmd('DiagnosticChanged', {
group = vim.api.nvim_create_augroup('dotfiles_diagnostics', { clear = true }),
Expand Down
2 changes: 1 addition & 1 deletion nvim/.config/nvim/lua/dotfiles/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ oil.setup {
},
}

vim.keymap.set('n', '-', oil.open, { silent = true })
vim.keymap.set('n', '-', oil.open)
28 changes: 10 additions & 18 deletions nvim/.config/nvim/lua/dotfiles/gitsigns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,24 @@ gitsigns.setup {
attach_to_untracked = false,
signs_staged_enable = false,
on_attach = function(bufnr)
local gs = package.loaded.gitsigns

local function map(mode, l, r, opts)
opts = opts or {}
opts.buffer = bufnr
vim.keymap.set(mode, l, r, opts)
local function map(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr })
end

map('n', ']c', function()
if vim.wo.diff then
return ']c'
vim.cmd.normal({ ']c', bang = true })
else
gitsigns.nav_hunk('next')
end
vim.schedule(function()
gs.next_hunk()
end)
return '<Ignore>'
end, { expr = true })
end)

map('n', '[c', function()
if vim.wo.diff then
return '[c'
vim.cmd.normal({ '[c', bang = true })
else
gitsigns.nav_hunk('prev')
end
vim.schedule(function()
gs.prev_hunk()
end)
return '<Ignore>'
end, { expr = true })
end)
end,
}
2 changes: 1 addition & 1 deletion nvim/.config/nvim/lua/dotfiles/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local bufnr = args.buf
local function map(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, silent = true })
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr })
end
local telescope = require('telescope.builtin')

Expand Down
9 changes: 3 additions & 6 deletions nvim/.config/nvim/lua/dotfiles/mappings.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
local function map(mode, lhs, rhs, opts)
opts = vim.tbl_extend('force', { silent = true }, opts or {})
vim.keymap.set(mode, lhs, rhs, opts)
end
local map = vim.keymap.set

map('n', '#', [[:setlocal hlsearch | :normal! #<CR>]])
map('n', '*', [[:setlocal hlsearch | :normal! *<CR>]])
Expand All @@ -21,8 +18,8 @@ map('n', 'g#', [[:setlocal hlsearch | :normal! g#<CR>]])
map('n', 'g*', [[:setlocal hlsearch | :normal! g*<CR>]])
map('n', 'j', [[v:count == 0 && &ft != 'qf' ? 'gj' : 'j']], { expr = true })
map('n', 'k', [[v:count == 0 && &ft != 'qf' ? 'gk' : 'k']], { expr = true })
map('n', '{', [[:keepjumps normal! {<CR>]])
map('n', '}', [[:keepjumps normal! }<CR>]])
map('n', '{', [[:keepjumps normal! {<CR>]], { silent = true })
map('n', '}', [[:keepjumps normal! }<CR>]], { silent = true })
map('t', '<Esc>', [[<C-\><C-n>]])

if vim.fn.has('nvim-0.11') == 0 then
Expand Down
4 changes: 1 addition & 3 deletions nvim/.config/nvim/lua/dotfiles/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ telescope.setup {

telescope.load_extension('fzf')

local function map(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, { silent = true })
end
local map = vim.keymap.set

map('n', '<Leader>o', builtin.find_files)
map('n', '<Leader>O', function()
Expand Down
2 changes: 1 addition & 1 deletion nvim/.config/nvim/lua/dotfiles/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ treesj.setup {
max_join_length = 200,
}

vim.keymap.set('n', '<Leader>x', treesj.toggle, { silent = true })
vim.keymap.set('n', '<Leader>x', treesj.toggle)

require('treesitter-context').setup {
enable = false,
Expand Down

0 comments on commit e03a304

Please sign in to comment.