A lightweight Neovim plugin for creating key mappings with buffer, filetype, and buftype scoping.
Most keymapping plugins give you global mappings. This plugin makes it trivial to create context-aware keymaps that only exist where you need them — per buffer, per filetype, or per buftype.
No more cluttering your config with autocmd boilerplate. Just declare where a keymap should apply.
- Buffer-local mappings — Keymaps that live only in the current buffer
- Filetype-specific mappings — Keymaps that activate only for specific file types
- Buftype-specific mappings — Keymaps for special buffer types (quickfix, terminal, etc.)
- Simple API with optional which-key integration
- Zero-dependency core (which-key optional)
- Global
Keymaptable for convenience
{
"sohanemon/keymap.nvim",
dependencies = { "folke/which-key.nvim" }, -- optional
opts = {},
}use("sohanemon/keymap.nvim")String (same icon for all):
require("keymap").setup({
icon = "", -- Icon for which-key (keymaps and groups)
})Table (different icons for keymaps and groups):
require("keymap").setup({
icon = {
default = "", -- Icon for keymaps
group = "", -- Icon for groups
},
})Or pass opts to lazy.nvim:
{
"sohanemon/keymap.nvim",
opts = {
icon = {
default = "",
group = "",
},
},
}Use either the module pattern or the global Keymap table:
-- Module pattern (recommended)
local keymap = require("keymap")
keymap.add({ ... })
-- Global pattern
Keymap.add({ ... })keymap.add({
key = "<leader>ff",
action = ":Telescope find_files<CR>",
desc = "Find files",
})Keymaps that live only in the current buffer:
keymap.add({
key = "<leader>q",
action = ":q<CR>",
desc = "Quit",
buffer = true,
})Keymaps that activate only for specific file types:
keymap.add({
key = "<leader>cc",
action = ":Commentary<CR>",
desc = "Comment",
filetype = "python",
})Multiple filetypes:
keymap.add({
key = "<leader>fm",
action = ":Format<CR>",
desc = "Format",
filetype = { "python", "lua", "javascript" },
})Keymaps for special buffer types:
keymap.add({
key = "<leader>x",
action = ":cclose<CR>",
desc = "Close quickfix",
buftype = "quickfix",
})keymap.add({
key = "<leader>cd",
action = ":cd %:p:h<CR>",
desc = "Change directory",
mode = { "n", "v" },
})keymap.add({
key = "<leader>rn",
action = ":lua vim.lsp.buf.rename()", -- Neovim command
vscode = "editor.action.rename", -- VSCode command
desc = "Rename",
})keymap.add({
key = "<leader>pp",
action = function()
vim.notify("Project panel")
end,
desc = "Show project panel",
})When action is omitted, creates a which-key group with desc as the group name:
-- Group leader (no action, just a label)
keymap.add({
key = "<leader>f",
desc = "Telescope files",
})
-- Actual mappings under the group
keymap.add({
key = "<leader>ff",
action = ":Telescope find_files",
desc = "Find files",
})
keymap.add({
key = "<leader>fb",
action = ":Telescope buffers",
desc = "Buffers",
})keymap.send_key("<Esc>", "i")keymap.delete("<leader>x", "n")Create a key mapping. If action is omitted, creates a which-key group with desc as the group name.
| Option | Type | Description |
|---|---|---|
key |
string |
Key sequence (required) |
action |
string|function |
Command or function (optional - omit to create a group) |
buffer |
boolean|number |
Buffer-local mapping |
filetype |
string|string[] |
Filetype pattern(s) |
buftype |
string|string[] |
Buftype pattern(s) |
mode |
string|string[] |
Mode(s): "n", "i", "v", "x", "s", "o", "t" (default: "n") |
desc |
string |
Description for which-key, or group name if action is omitted |
remap |
boolean |
Allow remapping |
icon |
string |
Icon for which-key (only used if which-key is available) |
vscode |
string |
VSCode command |
Send key sequence to Vim.
Delete an existing key mapping.
Configure plugin defaults.
MIT