13

I want to reload my neovim configuration files with just a couple of keystrokes instead of having to restart the app. I was able to do this when using an init.vim with the following command:

nnoremap <leader>sv <cmd>source $MYVIMRC<CR>

$MYVIMRC points correctly to my config entry point.

The problem is that I switched to using lua, and now I can't do the same. 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.

I know that plenary includes a function to reload modules, but I don't understand how to use it. A complete example of that would be good too since I already use plenary in my config.

alexfertel
  • 925
  • 1
  • 9
  • 22

1 Answers1

1

I am a new Neovim user, so I guess my solution may not work for some edge cases.

This function flushes the module of current buffer:

local cfg = vim.fn.stdpath('config')
Flush = function()
    local s = vim.api.nvim_buf_get_name(0)
    if string.match(s, '^' .. cfg .. '*') == nil then
        return
    end
    s = string.sub(s, 6 + string.len(cfg), -5)
    local val = string.gsub(s, '%/', '.')
    package.loaded[val] = nil
end

You can call it whenever you write to a buffer with this autocommand:

autocmd BufWrite *.lua,*vim call v:lua.Flush()

This way, after you execute :source $MYVIMRC it will also reload changed Lua modules.