[英]How can I reload my Neovim config written in lua with a shortcut?
I want to reload my neovim
configuration files with just a couple of keystrokes instead of having to restart the app.我想通过几次按键重新加载我的neovim
配置文件,而不必重新启动应用程序。 I was able to do this when using an init.vim
with the following command:使用带有以下命令的init.vim
时,我能够做到这一点:
nnoremap <leader>sv <cmd>source $MYVIMRC<CR>
$MYVIMRC
points correctly to my config entry point. $MYVIMRC
正确指向我的配置入口点。
The problem is that I switched to using lua
, and now I can't do the same.问题是我改用lua
,现在我不能这样做。 I have read the docs and tried variants of the following without success:我已阅读文档并尝试了以下变体但没有成功:
util.nnoremap("<leader>sv", "<cmd>luafile $MYVIMRC<CR>")
Finally, I found a solution doing this:最后,我找到了一个解决方案:
function load(name)
local path = vim.fn.stdpath('config') .. '/lua/' .. name .. '.lua'
dofile(path)
end
load('plugins')
load('config/mapping')
load('lsp/init')
Which is buggy and feels wrong.这是错误的,感觉不对。
Is there any way to do this?有没有办法做到这一点? I read the example in vimpeccable
, but I want to see the other available options since I would rather not install another plugin.我阅读了vimpeccable
的示例,但我想查看其他可用选项,因为我不想安装另一个插件。
I know that plenary
includes a function to reload modules, but I don't understand how to use it.我知道plenary
包含一个重新加载模块的功能,但我不明白如何使用它。 A complete example of that would be good too since I already use plenary
in my config.一个完整的例子也很好,因为我已经在我的配置中使用了plenary
。
I ended up going for the following:我最终进行了以下操作:
I have a util.lua
file that defines de following:我有一个util.lua
文件,它定义了以下内容:
_G.load = function(file)
require("plenary.reload").reload_module(file, true)
return require(file)
end
Then, instead of writing require(...)
, I write load(...)
and the only file I require is util.lua
, which is also the first one.然后,我没有写require(...)
,而是写了load(...)
并且我需要的唯一文件是util.lua
,这也是第一个。
Then I have the following mapping defined:然后我定义了以下映射:
util.nnoremap("<leader>sv", "<cmd>luafile $MYVIMRC<CR>")
Which reloads my whole config.这会重新加载我的整个配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.