arnvald / viml-to-lua

98 stars 8 forks source link

Suggested map function improvemet #2

Closed voyeg3r closed 1 year ago

voyeg3r commented 2 years ago

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>')
arnvald commented 2 years ago

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 👍