viml-to-lua icon indicating copy to clipboard operation
viml-to-lua copied to clipboard

Suggested map function improvemet

Open voyeg3r opened this issue 4 years ago • 1 comments

With this improvement the "options" are "optional", in that case the default ones are used

-- -- https://www.notonlycode.org/neovim-lua-config/
-- shortcut -> lhs     command -> rhs
-- lhs -> left hand side  rhs -> right rand side
function map(mode, shortcut, command, opts)
  local options = { noremap = true }
  if opts then
    options = vim.tbl_extend("force", options, opts)
  end
  vim.api.nvim_set_keymap(mode, shortcut, command, options)
end

function nmap(shortcut, command, opts)
  map("n", shortcut, command)
end

function imap(shortcut, command, opts)
  map("i", shortcut, command)
end

function vmap(shortcut, command, opts)
  map("v", shortcut, command)
end

-- you can map like this
nmap("<F10>", ':echom "just a test"<cr>')

nmap("<F4>", ":PackerLoad undotree<cr>:UndotreeToggle<cr>", { silent = true })

nmap("<F11>", "<cmd>lua require('utils').flash_cursorline()<cr>", { silent = true })

-- :lua nmap('<F12>', ':echo "It works great!"<cr>')

voyeg3r avatar Dec 07 '21 18:12 voyeg3r

Hey, thanks, that's a good suggestion! I hardcoded the options in my function just because I always use silent and noremap, but if someone needs more flexibility that's a good tip 👍

arnvald avatar Feb 11 '22 19:02 arnvald