/mappy.nvim

A tiny utility to call any *map commands.

Primary LanguageLua

mappy.nvim

A tiny utility to call any *map commands.

What's this?

In Vimscript, we can do this.

nmap <C-u> :echo 'Hello, World!'<CR>
imap <C-t> <buffer> <expr> strftime('%FT%T') 

In addition to this, Neovim has two functions for Lua -- nvim_set_keymap, nvim_buf_set_keymap --, and they are so annoying.

vim.api.nvim_set_keymap('n', '<C-u>', [[:echo 'Hello, World!']], {})
vim.api.nvim_buf_set_keymap(0, '<C-t>', [[strftime('%FT%T')]], {expr = true})

Too complex and too long syntax. mappy.nvim solves this.

local mappy = require'mappy'
mappy.nmap('<C-u>', [[:echo 'Hello, World!']])
mappy.imap({'expr', 'buffer'}, [[strftime('%FT%T')]])

So simple!

Features

map functions

mappy.nvim supplies functions below.

All functions
mappy.nvimequivalent in Vimscript
mapmap
map_map!
nmapnmap
vmapvmap
xmapxmap
smapsmap
omapomap
imapimap
lmaplmap
cmapcmap
tmaptmap
noremapnoremap
noremap_noremap!
nnoremapnnoremap
vnoremapvnoremap
xnoremapxnoremap
snoremapsnoremap
onoremaponoremap
inoremapinoremap
lnoremaplnoremap
cnoremapcnoremap
tnoremaptnoremap
unmapunmap
unmap_unmap!
nunmapnunmap
vunmapvunmap
xunmapxunmap
sunmapsunmap
ounmapounmap
iunmapiunmap
lunmaplunmap
cunmapcunmap
tunmaptunmap

Names of functions are almost the same as Vimscript's ones. An exception for this exists. It is map! (and noremap!, unmap!) -- in mappy.nvim, you can use map_ (and noremap_, unmap_) for that.

map to Lua functions

mappy.nvim can map keys to Lua functions! This cannot do with the original nvim_set_keymap, nvim_buf_set_keymap.

mappy.imap({'buffer'}, '<C-t>', function()
  print'Hello, World!'
end)

Limitation

Lua functions cannot be mapped to <expr> mappings.

Utility functions to map one to multiple keys

You can use bind() for this.

mappy.bind('nit', {'buffer'}, '<A-t>', function()
  print(os.date('%FT%T'))
end)

This can call the function with <A-t> in normal, insert, and terminal modes.

bind() does mappings with noremap = true. You can use recursive mappings with rbind().

mappy.rbind('nv', {'buffer'}, '<C-t>', 'n')

Utility function to map for buffers

add_buffer_maps() calls functions to run mappy with {'buffer'}.

mappy.add_buffer_maps(function()
  mappy.imap({'expr'}, '<C-t>', [[strftime("%FT%T")]])
end)

This adds {'buffer'} and call nvim_buf_set_keymap automatically.

See also

svermeulen/vimpeccable
Actually, this plugin is a subset of vimpeccable. I imitated the mapping feature only from it.
Mappy - Wikipedia
Mappy is a great game. (no related for this plugin ;)