采购nvim.init并不采购所需的lua文件

发布于 2025-01-28 22:34:10 字数 528 浏览 3 评论 0原文

我有Neovim 0.7.0运行,我的.vimrc在〜/.config/nvim/init.vim

我也有以下文件:〜/.config/nvim/nvim/lua/statusline.lua,带有一行代码: print('来自statusline.lua的消息')

insion.vim我有:

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

当我启动nvim时,我会收到两个消息('来自init.vim''和'statusline.lua'的消息))这就是我所期望的。

当我运行时:源$ myvimrc,我只会看到'init.vim'。我希望其他消息(来自statusline.lua'的消息)也会出现。

我认为这意味着我在statusline中进行的任何更改。当我运行:source $ myvimrc时,LUA都不会生效。我应该如何在不关闭和重新启动Neovim的情况下源init.vim文件加上所需的任何文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十级心震 2025-02-04 22:34:10

通过附加将false返回到模块的末尾,要么通过附加将缓存条目无效。

或者根本不使用完全不需要,因为无论如何您都不需要缓存或路径搜索。例如

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

PS LUA不是“配置工具”。这是一种功能齐全的编程语言。如果您不喜欢通过正确学习时间来浪费时间(即阅读书籍和教程),则强烈建议您使用vimscript。它具有一些自己的“黑暗角落”,但更适合编写配置。

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.

毁梦 2025-02-04 22:34:10

这取决于您的statusline.lua的设计以及有关LUA'模块加载程序系统的知识。
看起来,因为我必须围绕statusline.lua烦恼,它没有任何返回。
因为返回已进入软件包。已加载和同一会话中的同样的要求首先在此处查找statusline
因此,尝试一下...

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

我已经在上面测试了...

$ 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

编辑
另一种用于做有用的事情的设计...

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

和...

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

return dump

比您有一个表查看器,您可以看到功能和表的来自...

  1. :lua dump(_g) -全局环境表
  2. :LUA DUMP(VIM) - NVIM Stuff(aka模块)
  3. :lua dump(vim.api) - NVIM API函数(AKA库)
  4. :Lua dump(jit) - 正义编译器;-)
  5. :lua dump([table name]) - 任何看起来有趣的表格
  6. : lua dump(package.poaded) - 所需或需要的东西
    可以执行上述函数,而无需定义dump首先使用::lua require('dump')(_ g)
    因此:首先需要加载dump.lua到package.loaded.dump中并返回它,每一个需要返回:package.loaded.dump

    如果您的眼睛敏锐,请看一下_G.DUMP,这只是一个引用(指针/链接)到package.loaded.dump。

edit2
准备dump.lua将其与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

> 'i')vim.api.nvim_input(dump(vim.api))

由于许多nvim api函数返回表,dump函数变得很方便...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文