0

In "~/.config/nvim/lua/foo.lua" I have the following

local M = {}

function M.test()
  vim.notify("hello")
end
return M

And then in my init.lua I have

local test = require('foo').test
vim.keymap.set('n', "gh", test)

If I start up nvim, everything works as expected. However, if I change the string that test notifies and then source my init.lua, it still messages "hello". Is there a way to "re-source" my other lua modules when I source my init.lua?

1 Answer 1

1

To force require 'foo' again, you can set package.loaded['foo'] = nil.

Therefore, if you do this in your init.lua, it reloads 'foo' every time automatically.

package.loaded['foo'] = nil
local test = require('foo').test
vim.keymap.set('n', "gh", test)

It is likely that you would require multiple packages in your config. A helper function would be useful.

local function import(module)
    package.loaded[module] = nil
    return require(module)
end

local test = import('foo').test
vim.keymap.set('n', "gh", test)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.