如何在不重新启动Neovim的情况下来源init.lua

发布于 2025-02-02 01:43:58 字数 411 浏览 5 评论 0原文

是否有一种干净的方法来重新加载neovim init.lua配置及其所有模块(使用require()函数)而无需重新启动Neovim?

我已经在另一篇文章上读到:luafile $ myvimrc应该做到这一点,但不幸的是,它并没有重新加载那些缓存的文件。我希望像以前的init.vim config一样设置keymap。沿着:

local opts = { noremap = true, silent = true }

vim.api.nvim_set_keymap("n", "<leader><CR>", ":luafile $MYVIMRC<CR>", opts)

我在NVIM v0.8.0上。

Is there a clean way to reload a neovim init.lua config and all its modules (using the require() function) without restarting neovim?

I've read on another post that :luafile $MYVIMRC was supposed to accomplish just that but it doesn't reload those cached files unfortunately. I'm hoping to setup a keymap like I used to have in my previous init.vim config. Something along the lines of:

local opts = { noremap = true, silent = true }

vim.api.nvim_set_keymap("n", "<leader><CR>", ":luafile $MYVIMRC<CR>", opts)

I'm on nvim v0.8.0.

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

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

发布评论

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

评论(7

迷乱花海 2025-02-09 01:43:58

尝试运行此命令:

:luafile %

Try to run this command :

:luafile %
黑白记忆 2025-02-09 01:43:58

如果从此reddit线程这似乎很好。我最终创建了一个称为reload.lua的小型模块:

function _G.ReloadConfig()
  for name,_ in pairs(package.loaded) do
    if name:match('^user') and not name:match('nvim-tree') then
      package.loaded[name] = nil
    end
  end

  dofile(vim.env.MYVIMRC)
  vim.notify("Nvim configuration reloaded!", vim.log.levels.INFO)
end

init.lua 中导入。

require 'user.reload'

vim.api.nvim_set_keymap("n", "<leader><CR>", "<cmd>lua ReloadConfig()<CR>", { noremap = true, silent = false })

它 LUA文件需要包含在A 用户文件夹中:〜/.config/nvim/lua/user/。这也是reload.lua生活的地方。

注2:我认为可以使用而不是名称:match('dublude-me') REGEX语法来排除问题模块。

If found an answer from creativenull on this reddit thread that seems to work well. I ended up creating a small module called reload.lua:

function _G.ReloadConfig()
  for name,_ in pairs(package.loaded) do
    if name:match('^user') and not name:match('nvim-tree') then
      package.loaded[name] = nil
    end
  end

  dofile(vim.env.MYVIMRC)
  vim.notify("Nvim configuration reloaded!", vim.log.levels.INFO)
end

That gets imported in init.lua:

require 'user.reload'

And for which I added a keymap:

vim.api.nvim_set_keymap("n", "<leader><CR>", "<cmd>lua ReloadConfig()<CR>", { noremap = true, silent = false })

Note 1: in the example above your lua files need to be contained in a user folder: ~/.config/nvim/lua/user/. That's also where reload.lua lives.

Note 2: I think it's possible to use the not name:match('exclude-me') regex syntax to exclude problematic modules.

桃气十足 2025-02-09 01:43:58

您可以使用:luafile&lt; filename&gt;
请参阅:H:Luafile有关更多信息。

You can use :luafile <filename> for this.
See :h :luafile for more information.

清泪尽 2025-02-09 01:43:58

这是另一个解决方案,还包括在/之后重新加载文件:

我的〜/.config/nvim文件夹树:

.
├── after
│  ├── ftplugin
│  └── plugin
│     ├── telescope.lua
│     └── treesitter.lua
├── init.lua
├── lua
│  └── user
│     ├── init.lua
│     ├── maps.lua
│     ├── options.lua
│     └── plugins.lua
└── plugin
   └── packer_compiled.lua

我的〜/.config/nvim。 init.lua

require('user')


--- Reload the entire configuration
function reload_config()
    for name,_ in pairs(package.loaded) do
        if name:match('^user') then
            package.loaded[name] = nil
        end
    end

    require('user')

    -- Reload after/ directory
    local glob = vim.fn.stdpath('config') .. '/after/**/*.lua'
    local after_lua_filepaths = vim.fn.glob(glob, true, true)

    for _, filepath in ipairs(after_lua_filepaths) do
        dofile(filepath)
    end

    vim.notify("Nvim configuration reloaded!", vim.log.levels.INFO)
end
vim.keymap.set('n', '<leader><leader><leader>x', reload_config)

Here's another solution that also includes reloading the files in the after/ directory:

My ~/.config/nvim folder tree:

.
├── after
│  ├── ftplugin
│  └── plugin
│     ├── telescope.lua
│     └── treesitter.lua
├── init.lua
├── lua
│  └── user
│     ├── init.lua
│     ├── maps.lua
│     ├── options.lua
│     └── plugins.lua
└── plugin
   └── packer_compiled.lua

my ~/.config/nvim.init.lua:

require('user')


--- Reload the entire configuration
function reload_config()
    for name,_ in pairs(package.loaded) do
        if name:match('^user') then
            package.loaded[name] = nil
        end
    end

    require('user')

    -- Reload after/ directory
    local glob = vim.fn.stdpath('config') .. '/after/**/*.lua'
    local after_lua_filepaths = vim.fn.glob(glob, true, true)

    for _, filepath in ipairs(after_lua_filepaths) do
        dofile(filepath)
    end

    vim.notify("Nvim configuration reloaded!", vim.log.levels.INFO)
end
vim.keymap.set('n', '<leader><leader><leader>x', reload_config)
合久必婚 2025-02-09 01:43:58

我不确定。我正在使用映射:W |因此%到源当前文件
源文件keymap

-update-
这仅在像这样的配置文件中起作用,

希望有这样的帮助

I am not sure yet. I am using mapping :w | so % to source current file
sourcing file keymap

--update --
this works in only config file

like this hope this help

鱼窥荷 2025-02-09 01:43:58

解决方案>解决方案在Neovim v0.7.7.7.7.7.7.7.2上对我有用。

-- Add modules here
local modules = {
  "user.options",
  "user.keymaps",
}

-- Refresh module cache
for k, v in pairs(modules) do
  package.loaded[v] = nil
  require(v)
end

然后,您可以将您的keymap配置为刷新$ myvimrc,如下所示:

vim.api.nvim_set_keymap("n", "<leader><CR>", ":luafile $MYVIMRC<CR>", opts)

This solution works for me on Neovim v0.7.2.

-- Add modules here
local modules = {
  "user.options",
  "user.keymaps",
}

-- Refresh module cache
for k, v in pairs(modules) do
  package.loaded[v] = nil
  require(v)
end

You can then config your keymap to refresh $MYVIMRC as follows:

vim.api.nvim_set_keymap("n", "<leader><CR>", ":luafile $MYVIMRC<CR>", opts)
墨落画卷 2025-02-09 01:43:58

:源将做到。它也可以缩短为:so

请参阅:H:Source有关更多信息。

:source will do it. It can also be shortened to :so.

See :h :source for more info.

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