Bits and bytes of code
Bytes is my collection of short-form posts, tips, and things I learn as I build software.
Bytes is my collection of short-form posts, tips, and things I learn as I build software.
Neovim’s vim.keymap.set and similar utilities are very useful for
creating and managing keymaps, however they lack some ergonomics when
building more complex keymaps that require setting and deleting the keymaps
periodically. A simply utility function makes this really clean and easy to
do.
I’ve called this function map_fn (because I honestly couldn’t come up
with a better name), and when called it will return a class containing
chainable set and del methods.
--- @class Map
--- @field set fun(): Map
--- @field del fun(): Map
--- Creates a keymap function
--- @param mode string|table
--- @param key string
Combining this with the
augroup function I created
in a previous Byte, we can build a complex mapping and autocmd setup like
this with very little code:
local command_line = map_fn("n", "q;", "q:", "Open command line window"):set()
augroup("command_line", function(autocmd)
autocmd("RecordingEnter", { callback = command_line.del })
autocmd("RecordingLeave", { callback
What this does is create a mapping q; which is equivalent to q: to open
the command line window. However, because the macro recording stop keymap
is q, we only want to enable this mapping when recording is not active,
so our autocmd will handle deleting and re-creating the keymap for us when
the autocmds fire.