4

I have neovim 0.7.0 running and my .vimrc is at ~/.config/nvim/init.vim

I also have the following file: ~/.config/nvim/lua/statusline.lua with one line of code: print('message from statusline.lua')

Inside init.vim I have:

echo 'from init.vim'
lua require('statusline')

When I start nvim I get both messages printed out ('from init.vim' and 'message from statusline.lua') which is what I would expect.

When I run :source $MYVIMRC I only see 'from init.vim'. I would expect the other message ('message from statusline.lua') to appear as well.

I assume this means any changes I make in statusline.lua will not take effect when I run :source $MYVIMRC. How should I source my init.vim file plus any files it requires without closing and restarting neovim?

1
  • As documented in :help lua-guide-modules (recommended read), Lua caches the modules on first require. So you will need to invalidate the cache whenever there is any change in runtime. Run :lua package.loaded['statusline'] = nil. Or use dofile as an alternative. Commented Apr 23, 2023 at 16:44

2 Answers 2

4

Either invalidate cache entry by appending return false to the end of a module.

Or don't use require at all, as you need neither cache nor path search anyway. E.g.

for k, v in ipairs(vim.fn.glob("~/.config/nvim/init.d/*.lua", false, true)) do
    dofile(v)
end

P.S. Lua is not a "config tool". It is a full-featured programming language. If you don't like wasting your time by learning it properly (i.e. reading books and tutorials) you're highly suggested to use VimScript instead. It has some "dark corners" of its own but it is much better suited for writing config.

Sign up to request clarification or add additional context in comments.

3 Comments

I have verified that adding return false does result in the message from statusline.lua being displayed when I source $VIMRC. I think I will take your advice and stick to VimScript.
I'm obliged to agree with that "P.S." statement.
On "learning it properly": that's an implicit P.S. to probably each and every SO answer. A bit arrogant.
2

It depends on the design of your statusline.lua and knowledge about Lua' module loader system.
It looks, because i have to riddle about statusline.lua, that it nothing returns.
Because the return is going into package.loaded and same require in same session looks first there for statusline
So give following a try...

-- statusline.lua
print('message from statusline.lua')
return 'message from package.loaded.statusline'

I have tested above with...

$ lua -i
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> require('statusline')
message from statusline.lua
message from package.loaded.statusline  ./lua/statusline.lua
> require('statusline')
message from package.loaded.statusline
> require('statusline')
message from package.loaded.statusline

EDIT
Another design for doing something usefull...

-- ~.config/nvim/init.vim
lua print("init.vim")
lua dump = require("dump")

And...

-- ~/.config/nvim/lua/dump.lua
local dump = function(tab)
for key, value in pairs(tab) do
 print(key, '=>', value)
end
end

return dump

Than you have a table viewer and you can see where the functions and tables come from with...

  1. :lua dump(_G) -- The global environment table
  2. :lua dump(vim) -- The nvim stuff (aka Module)
  3. :lua dump(vim.api) -- The nvim API functions (aka Library)
  4. :lua dump(jit) -- The Just In Time Compiler ;-)
  5. :lua dump([Table Name]) -- Any table that looks interesting
  6. :lua dump(package.loaded) -- The required or requireable stuff
    Above function can be executed without defining dump first with: :lua require('dump')(_G)
    So: First require loads dump.lua into package.loaded.dump and returning it and every further require returning: package.loaded.dump
    If you have an sharp eye than take a look on _G.dump thats only a reference (pointer/link) to package.loaded.dump.

EDIT2
Preparing dump.lua for using it with vim.api.nvim_input()

-- ~/.config/nvim/lua/dump.lua
local dump = function(tab)
local tmp = ''
for key, value in pairs(tab) do
 tmp = tmp .. ('%s %s %s\n'):format(key, '=>', value)
end
return tmp
end

return dump

Now the dump function returning a string and the output can be loaded into nvim with: :lua vim.api.nvim_input('i') vim.api.nvim_input(dump(vim.api))

Since many nvim API functions returning a table the dump function becomes handy with...
enter image description here

5 Comments

I added the second line to statusline.lua and recreated your results on the command line. However, when I open up neovim I only get 'message from statusline.lua' not the new message. When I run :source $MYVIMRC I get neither message showing up. My knowledge of Lua is minimal. I am not quite sure what is going on. Your answer has helped me get some insight.
Try (init.vim): lua print(require('statusline')) because above statusline.lua returning only a string that should put out on stdout. The Lua Standalone (console) do a return for each Enter/Return thats a lazy way to save a print() for outputing something.
I put lua print(require('statusline')) in init.vim and now it prints true (ie. the boolean value) when I source my init.vim. It does that even when I delete both the print and return statements from statusline.lua. I guess it is printing the return value of the require function.
I check this... - I do in init.vim lua print(require("statusline")) and in statusline.lua return 'message from statusline' and that worked if statusline.lua in same folder where you started nvim. ( only by starting nvim and with command :source $MYVIMRC )
thanks for your help. I am very out of my depth with Lua and think I will stick to VimScript for the moment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.