0

I want to execute a imported function on a button click, but I get an error that its undefined.

local LazyComp = require('plugins/LazyComp/LazyComp')
vim.api.nvim_set_keymap('n', '<Tab>', "LazyComp.getFile()<cr>", {expr = true, noremap = true})

2 Answers2

2

You need to provide a vim script expression in that mapping. Not a Lua function call. There's no way to get that local variable called from nvim through a string.

I'm no expert in nvim's Lua API but 1 minute of websearch gave me this solution:

vim.api.nvim_set_keymap('n', '<TAB>', "<cmd>lua require('plugins/LazyComp/LazyComp').getFile()<CR>")

Alternatively you need to make your module available globally.

_G.LazyComp = require('plugins/LazyComp/LazyComp')

Then you should be able to access the global environemt through v:lua

vim.api.nvim_set_keymap('n', '<TAB>', "v:lua.LazyComp.getFile()<CR>")

Both untested.

Piglet
  • 27,501
  • 3
  • 20
  • 43
2

Modern way for keymaps is to use vim.keymap.set:

vim.keymap.set('n', 'lhs', function() print("real lua function") end)
run_the_race
  • 1,344
  • 2
  • 36
  • 62