Task
How do you set a keybinding in Vim using Lua?
Feedback
Syntax
mode: The mode for the keybinding (e.g., ‘n’ for normal, ‘i’ for insert).
keys: The key combination to trigger the command.
command: The Vim command or Lua function to run.
explanation: Documentation of the keymap (optional)
Example and Details
Add the keybinding to either:
~/.config/nvim/init.lua — for a single-file config, or
~/.config/nvim/lua/keybindings.lua — if you organize configs by module.
Example keybinding:
vim.keymap.set('n', '<leader>cd', ':cd /Users/donaldelger/Documents/Vault/Notes<CR>')
-- Change directory to Notes
Explanation
‘n’: This binding works in normal mode.
‘
cd’: The shortcut you press (by default, unless you’ve changed your ). :cd /Users/donaldelger/Documents/Vault/Notes<CR>
: Runs the Vim :cd command to change the current directory.<CR>
simulates pressing Enter.
This lets you quickly switch to your Vault Notes directory by pressing in normal mode.