10

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.

mduboule
  • 161
  • 1
  • 1
  • 10

6 Answers6

14

Try to run this command :

:luafile %
Cyrius
  • 329
  • 2
  • 4
  • 2
    Thanks for chiming in. Doesn't `%` target the active buffer though? I'm trying to assign a keymap to source my init.lua file (including modules) from any location. – mduboule May 28 '22 at 21:41
6

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.

mduboule
  • 161
  • 1
  • 1
  • 10
3

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Right, the issue I'm facing is that modules are cached when using the `require()` import and simply executing the `:luafile` command doesn't reload those modules. Will update my post to clarify this point. – mduboule Jun 05 '22 at 02:02
1

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)
Yanis.F
  • 612
  • 6
  • 18
0

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

lastfor
  • 98
  • 4
  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 16:22
  • This answer didn't work for me when I have a keymap, comment it out and type `:w | so %`. This seems to work when modifying or adding additional keymap lines, but not removing them or commenting them out. – Ryan Hart Feb 01 '23 at 13:22
0

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)
  • Tell me if I'm wrong but this doesn't clear the cache does it? – mduboule Jul 05 '22 at 16:33
  • @mduboule You seem to be right about that. I just tested this in my init.lua, and when I comment out lines of my keymaps, for example and then `:source %` it, my changes still down take effect, but if I were to make changes to what the key actually does, and `:source %`, then my changes go into effect. – Ryan Hart Feb 01 '23 at 12:37