mduboule
mduboule

Reputation: 420

How to source init.lua without restarting neovim

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.

Upvotes: 24

Views: 28677

Answers (7)

thodic
thodic

Reputation: 2259

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

See :h :source for more info.

Upvotes: 0

Yanis.F
Yanis.F

Reputation: 684

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)

Upvotes: 1

user19222567
user19222567

Reputation:

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

Upvotes: 5

yungbool
yungbool

Reputation: 1

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)

Upvotes: 0

lastfor
lastfor

Reputation: 98

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

Upvotes: 0

mduboule
mduboule

Reputation: 420

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.

Upvotes: 8

Cyrius
Cyrius

Reputation: 421

Try to run this command :

:luafile %

Upvotes: 20

Related Questions