toggle.lua (752B)
1 local M = {} 2 3 local keymap_prefix = 'yo'; 4 5 function M.option(letter, option, mode) 6 vim.keymap.set('n', keymap_prefix .. letter, function() 7 local scope 8 if mode == 'local' then 9 scope = vim.opt_local 10 elseif mode == 'global' then 11 scope = vim.opt_global 12 else 13 scope = vim.opt 14 end 15 16 local val = scope[option]:get() 17 if type(val) == 'boolean' then 18 scope[option] = not val 19 elseif type(val) == 'number' then 20 if val == 1 then 21 scope[option] = 0 22 else 23 scope[option] = 1 24 end 25 end 26 end, { desc = 'Toggle ' .. option }) 27 end 28 29 function M.fn(letter, fn, desc) 30 vim.keymap.set('n', keymap_prefix .. letter, function() 31 fn() 32 end, { desc = 'Toggle ' .. desc }) 33 end 34 35 return M