0

I am trying to update the config and see the changes take place without exiting neovim.

I have structured my neovim config files like this

~/.config/nvim
├── init.lua
└── lua
    └── [username]
        ├── core
        │   └── options.lua
        ├── init.lua
        └── plugins

These are the contents of my files

-- init.lua

require("[username]")
-- ./lua/[username]/init.lua

require("[username].core.options")
-- ./lua/[username]/core/options.lua

local opt = vim.opt -- aliasing

-- line number
opt.number = true

-- tabs & intendation
opt.tabstop = 4
opt.shiftwidth = 4
opt.expandtab = true
opt.autoindent = true

I have windows like this

        +----------------------------------------------------------------------------------+
        |-- ./lua/[username]/init.lua         |-- ./lua/[username]/core/options.lua        |
        |                                     |                                            |
        |require("[username].core.options")   |local opt = vim.opt -- aliasing             |
        |~                                    |                                            |
        |~                                    |-- line number                              |
        |~                                    |opt.number = false                          |
        |~                                    |                                            |
        |~                                    |-- tabs & intendation                       |
        |~                                    |opt.tabstop = 4                             |
        |~                                    |opt.shiftwidth = 4                          |
        |~                                    |opt.expandtab = true                        |
        |~                                    |opt.autoindent = true                       |
        |~                                    |~                                           | 
        |init.lua=============================options.lua==================================|
        |                                                                                  |
        +----------------------------------------------------------------------------------+

When I make change in window with options.lua and do :so, the changes are reflected in that window 2 and not window 1 with init.lua. When I switch to that window and do :so nothing happens. Then I tried to do :so $MYVIMRC in both windows and nothing happened.

I tried :e in window 1, even closed the window, and opened it again with :vs lua/[username]/init.lua. The changes in the config were not applied. Then I tried :bdel lua/[username]/init.lua and opened it again with :vs lua/[username]/init.lua and now the window had the configs applied.

Why is this and how can I apply the changes to both window without having to even close the window?

3
  • Without having digested the question: my personal opinion is that for about 90% of config changes, you really should just quit and reopen. Using sessions can make doing so more ergonomic.
    – D. Ben Knoble
    Jun 11 at 18:13
  • @D.BenKnoble can you tell why it is better to quit and reopen?
    – me.nkr
    Aug 8 at 13:41
  • Because you cannot guarantee a truly clean slate without quitting.
    – D. Ben Knoble
    Aug 8 at 19:45

1 Answer 1

0

A solution that is not tested extensively has been found, thanks to this reddit post

here is how I did it

I created core/reload.lua

-- ./lua/[username]/core/reload.lua

-- utility function apply config without closing

vim.api.nvim_create_user_command('RC', function()
     for name,_ in pairs(package.loaded) do
         if name:match('^[username]') then
             package.loaded[name] = nil
         end
     end

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

and required it in [username]/init.lua to load it everytime

-- ./lua/[username]/init.lua

require("[username].core.options")
require("[username].core.reload")

The above code assigned the function to a user defined command RC

I am not 100% sure if this info is true, but when we call require the info is cached, so this function is pretty much clearing the cache and somewhat doing :source $MYVIMRC ( source for this info )

4
  • Just note that :source $MYVIMRC and equivalents do not and cannot wipe out all the "state" of your Vim instance (set options, variables, mappings, etc.). You might get lucky with functions, commands, autocommands, etc., because there are well-defined mechanisms for overwriting those. Similar with other stuff in $VIMRUNTIME, because those are designed to be somewhat more repeatable. But especially plugins, mappings, and local scripts are not going to be starting "clean," so the end state may be more confusing than helpful.
    – D. Ben Knoble
    Aug 8 at 19:51
  • It can overwrite options, variables, and mappings though right? Also, could you elaborate on the last statement?
    – me.nkr
    Sep 23 at 12:57
  • Overwrite, yes: but not "start from scratch"! Let's say my vimrc had :set number. Now I delete that line and :source $MYVIMRCnumber will still be set. What I am saying is that you cannot replicate the Vim startup sequence without, well, starting Vim anew (Common Lisp REPLs sometimes suffer from this, where the state of the REPL is so heavily influenced by prior code that it's hard to figure out what steps to take to get back to a known configuration).
    – D. Ben Knoble
    Sep 23 at 14:12
  • Ah ok, makes sense now. Even though in my case when I reload I will be setting new values to options. But yea I will keep this in mind because sooner or later I am gonna face this issue, Thanks for the info mate, have a good one : )
    – me.nkr
    Sep 28 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.