2

Is it possible to overwrite a .vimrc setting with a local one?

For example:

I have set background=light in my .vimrc file

And then, while editing another file I decide to change that setting, so I run the command :set background=dark in order to apply it to the current session.

Let's say I want to keep this setting. I should open my .vimrc file and make the change. And here is my question: can I make this change without visiting the .vimrc file?

2 Answers 2

1

There is nothing built-in that is specifically designed for that.

You can certainly do something like :!echo 'set background=dark' >> ~/.vimrc but I can only imagine the mess that would result from doing that a few times. Same for the built-in :help :mkvimrc, which is not granular at all.

2

You could always add a plugin to your runtime:

~/.vim/plugin/local_vimrc.vim

Another option would be to simply source a file in your vimrc file:

if filereadable(expand('~/.vimrc.local'))
  source ~/.vimrc.local
endif
3
  • 1
    That alone would not solve the question, would it? You would need to somehow (automatically?) write to the local vimrc. Editing an extra file is just as good as editing .vimrc directly unless I'm missing something.
    – Friedrich
    Jun 21, 2023 at 14:14
  • 2
    I think that depends on the needs. If your main vimrc is under version control, but you need a few settings on a specific machine then this method would allow you to keep your vimrc in sync and "override" a few settings. Unless I am missing the point, which is possible Jun 21, 2023 at 21:21
  • I see what you mean and that's one way to read the question. I understood the OP was looking for a way to persistently :set foo. I guess (at least) one of us is mistaken and only the OP could tell who it is. Thank you for clarifying.
    – Friedrich
    Jun 22, 2023 at 6:58

Your Answer

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

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