1
  • I opened a file in Vim.
  • Then opened vimrc file with another editor (VsCode) and made some changes.

I would like those changes to be reflected in my Vim session.

  • I tried :so % but it didn't work correctly.

Why?

How to have the changes in vimrc reflected in Vim without restarting Vim?

4
  • It should but depending how your vimrc is written re-running is not the same as running. Could you give an example? Could you come with a minimal vimrc that demonstrate the problem? Oct 3 at 21:04
  • 2
    It depends entirely on the changes made, too. Best is to restart vim.
    – D. Ben Knoble
    Oct 3 at 21:30
  • @VivianDeSmedt It was a large file, but I think I needed set autoread because set nu did not work in that file. Can you mention some changes that will not appear immediately? Oct 4 at 6:31
  • The options (e.g. autoread, number) should work fine. What is more tricky is the autocmd that depending how they are configured pileup instead of being replaced (augroup and ! flag can be used to control that). Oct 4 at 6:56

1 Answer 1

2

:so %, short for :source %, is:

  • the :help :source command, which is used to "source" vimscript files,
  • %, which is a shortcut for "the current file".

Therefore, it means "source the current file" or, at a lower level, "execute all the Ex commands from the current file".

If the current file is not a vimscript file, say you opened a .yaml file, then "source the current file" is a pointless thing to ask Vim to do because it is very unlikely to contain any vimscript.

From Vim, the proper way to source your vimrc is to use the $MYVIMRC environment variable (that is only set IN Vim):

:so $MYVIMRC

NOTE: As hinted in the comments, not everything you can put in your vimrc can be expected to have an immediate effect upon sourcing and settings can come from other places than your vimrc anyway, so the command above is not a silver bullet. Starting a new session is a much better solution.

NOTE: Editing your vimrc in a different editor makes no sense. Doing it in Vim gives you proper syntax highlighting, completion, and direct access to the documentation via :help K. And FWIW, it also allows you to do things like :w|so % while you are editing your vimrc.

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.